1/* Output Dwarf2 format symbol table information from GCC.
2 Copyright (C) 1992-2017 Free Software Foundation, Inc.
3 Contributed by Gary Funck (gary@intrepid.com).
4 Derived from DWARF 1 implementation of Ron Guilmette (rfg@monkeys.com).
5 Extensively modified by Jason Merrill (jason@cygnus.com).
6
7This file is part of GCC.
8
9GCC is free software; you can redistribute it and/or modify it under
10the terms of the GNU General Public License as published by the Free
11Software Foundation; either version 3, or (at your option) any later
12version.
13
14GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15WARRANTY; without even the implied warranty of MERCHANTABILITY or
16FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17for more details.
18
19You should have received a copy of the GNU General Public License
20along with GCC; see the file COPYING3. If not see
21<http://www.gnu.org/licenses/>. */
22
23/* TODO: Emit .debug_line header even when there are no functions, since
24 the file numbers are used by .debug_info. Alternately, leave
25 out locations for types and decls.
26 Avoid talking about ctors and op= for PODs.
27 Factor out common prologue sequences into multiple CIEs. */
28
29/* The first part of this file deals with the DWARF 2 frame unwind
30 information, which is also used by the GCC efficient exception handling
31 mechanism. The second part, controlled only by an #ifdef
32 DWARF2_DEBUGGING_INFO, deals with the other DWARF 2 debugging
33 information. */
34
35/* DWARF2 Abbreviation Glossary:
36
37 CFA = Canonical Frame Address
38 a fixed address on the stack which identifies a call frame.
39 We define it to be the value of SP just before the call insn.
40 The CFA register and offset, which may change during the course
41 of the function, are used to calculate its value at runtime.
42
43 CFI = Call Frame Instruction
44 an instruction for the DWARF2 abstract machine
45
46 CIE = Common Information Entry
47 information describing information common to one or more FDEs
48
49 DIE = Debugging Information Entry
50
51 FDE = Frame Description Entry
52 information describing the stack call frame, in particular,
53 how to restore registers
54
55 DW_CFA_... = DWARF2 CFA call frame instruction
56 DW_TAG_... = DWARF2 DIE tag */
57
58#include "config.h"
59#include "system.h"
60#include "coretypes.h"
61#include "target.h"
62#include "function.h"
63#include "rtl.h"
64#include "tree.h"
65#include "memmodel.h"
66#include "tm_p.h"
67#include "stringpool.h"
68#include "insn-config.h"
69#include "ira.h"
70#include "cgraph.h"
71#include "diagnostic.h"
72#include "fold-const.h"
73#include "stor-layout.h"
74#include "varasm.h"
75#include "version.h"
76#include "flags.h"
77#include "rtlhash.h"
78#include "reload.h"
79#include "output.h"
80#include "expr.h"
81#include "dwarf2out.h"
82#include "dwarf2asm.h"
83#include "toplev.h"
84#include "md5.h"
85#include "tree-pretty-print.h"
86#include "debug.h"
87#include "common/common-target.h"
88#include "langhooks.h"
89#include "lra.h"
90#include "dumpfile.h"
91#include "opts.h"
92#include "tree-dfa.h"
93#include "gdb/gdb-index.h"
94#include "rtl-iter.h"
95
96static void dwarf2out_source_line (unsigned int, unsigned int, const char *,
97 int, bool);
98static rtx_insn *last_var_location_insn;
99static rtx_insn *cached_next_real_insn;
100static void dwarf2out_decl (tree);
101
102#ifndef XCOFF_DEBUGGING_INFO
103#define XCOFF_DEBUGGING_INFO 0
104#endif
105
106#ifndef HAVE_XCOFF_DWARF_EXTRAS
107#define HAVE_XCOFF_DWARF_EXTRAS 0
108#endif
109
110#ifdef VMS_DEBUGGING_INFO
111int vms_file_stats_name (const char *, long long *, long *, char *, int *);
112
113/* Define this macro to be a nonzero value if the directory specifications
114 which are output in the debug info should end with a separator. */
115#define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 1
116/* Define this macro to evaluate to a nonzero value if GCC should refrain
117 from generating indirect strings in DWARF2 debug information, for instance
118 if your target is stuck with an old version of GDB that is unable to
119 process them properly or uses VMS Debug. */
120#define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 1
121#else
122#define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 0
123#define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 0
124#endif
125
126/* ??? Poison these here until it can be done generically. They've been
127 totally replaced in this file; make sure it stays that way. */
128#undef DWARF2_UNWIND_INFO
129#undef DWARF2_FRAME_INFO
130#if (GCC_VERSION >= 3000)
131 #pragma GCC poison DWARF2_UNWIND_INFO DWARF2_FRAME_INFO
132#endif
133
134/* The size of the target's pointer type. */
135#ifndef PTR_SIZE
136#define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
137#endif
138
139/* Array of RTXes referenced by the debugging information, which therefore
140 must be kept around forever. */
141static GTY(()) vec<rtx, va_gc> *used_rtx_array;
142
143/* A pointer to the base of a list of incomplete types which might be
144 completed at some later time. incomplete_types_list needs to be a
145 vec<tree, va_gc> *because we want to tell the garbage collector about
146 it. */
147static GTY(()) vec<tree, va_gc> *incomplete_types;
148
149/* A pointer to the base of a table of references to declaration
150 scopes. This table is a display which tracks the nesting
151 of declaration scopes at the current scope and containing
152 scopes. This table is used to find the proper place to
153 define type declaration DIE's. */
154static GTY(()) vec<tree, va_gc> *decl_scope_table;
155
156/* Pointers to various DWARF2 sections. */
157static GTY(()) section *debug_info_section;
158static GTY(()) section *debug_skeleton_info_section;
159static GTY(()) section *debug_abbrev_section;
160static GTY(()) section *debug_skeleton_abbrev_section;
161static GTY(()) section *debug_aranges_section;
162static GTY(()) section *debug_addr_section;
163static GTY(()) section *debug_macinfo_section;
164static const char *debug_macinfo_section_name;
165static GTY(()) section *debug_line_section;
166static GTY(()) section *debug_skeleton_line_section;
167static GTY(()) section *debug_loc_section;
168static GTY(()) section *debug_pubnames_section;
169static GTY(()) section *debug_pubtypes_section;
170static GTY(()) section *debug_str_section;
171static GTY(()) section *debug_line_str_section;
172static GTY(()) section *debug_str_dwo_section;
173static GTY(()) section *debug_str_offsets_section;
174static GTY(()) section *debug_ranges_section;
175static GTY(()) section *debug_frame_section;
176
177/* Maximum size (in bytes) of an artificially generated label. */
178#define MAX_ARTIFICIAL_LABEL_BYTES 30
179
180/* According to the (draft) DWARF 3 specification, the initial length
181 should either be 4 or 12 bytes. When it's 12 bytes, the first 4
182 bytes are 0xffffffff, followed by the length stored in the next 8
183 bytes.
184
185 However, the SGI/MIPS ABI uses an initial length which is equal to
186 DWARF_OFFSET_SIZE. It is defined (elsewhere) accordingly. */
187
188#ifndef DWARF_INITIAL_LENGTH_SIZE
189#define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
190#endif
191
192#ifndef DWARF_INITIAL_LENGTH_SIZE_STR
193#define DWARF_INITIAL_LENGTH_SIZE_STR (DWARF_OFFSET_SIZE == 4 ? "-4" : "-12")
194#endif
195
196/* Round SIZE up to the nearest BOUNDARY. */
197#define DWARF_ROUND(SIZE,BOUNDARY) \
198 ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
199
200/* CIE identifier. */
201#if HOST_BITS_PER_WIDE_INT >= 64
202#define DWARF_CIE_ID \
203 (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
204#else
205#define DWARF_CIE_ID DW_CIE_ID
206#endif
207
208
209/* A vector for a table that contains frame description
210 information for each routine. */
211#define NOT_INDEXED (-1U)
212#define NO_INDEX_ASSIGNED (-2U)
213
214static GTY(()) vec<dw_fde_ref, va_gc> *fde_vec;
215
216struct GTY((for_user)) indirect_string_node {
217 const char *str;
218 unsigned int refcount;
219 enum dwarf_form form;
220 char *label;
221 unsigned int index;
222};
223
224struct indirect_string_hasher : ggc_ptr_hash<indirect_string_node>
225{
226 typedef const char *compare_type;
227
228 static hashval_t hash (indirect_string_node *);
229 static bool equal (indirect_string_node *, const char *);
230};
231
232static GTY (()) hash_table<indirect_string_hasher> *debug_str_hash;
233
234static GTY (()) hash_table<indirect_string_hasher> *debug_line_str_hash;
235
236/* With split_debug_info, both the comp_dir and dwo_name go in the
237 main object file, rather than the dwo, similar to the force_direct
238 parameter elsewhere but with additional complications:
239
240 1) The string is needed in both the main object file and the dwo.
241 That is, the comp_dir and dwo_name will appear in both places.
242
243 2) Strings can use four forms: DW_FORM_string, DW_FORM_strp,
244 DW_FORM_line_strp or DW_FORM_GNU_str_index.
245
246 3) GCC chooses the form to use late, depending on the size and
247 reference count.
248
249 Rather than forcing the all debug string handling functions and
250 callers to deal with these complications, simply use a separate,
251 special-cased string table for any attribute that should go in the
252 main object file. This limits the complexity to just the places
253 that need it. */
254
255static GTY (()) hash_table<indirect_string_hasher> *skeleton_debug_str_hash;
256
257static GTY(()) int dw2_string_counter;
258
259/* True if the compilation unit places functions in more than one section. */
260static GTY(()) bool have_multiple_function_sections = false;
261
262/* Whether the default text and cold text sections have been used at all. */
263
264static GTY(()) bool text_section_used = false;
265static GTY(()) bool cold_text_section_used = false;
266
267/* The default cold text section. */
268static GTY(()) section *cold_text_section;
269
270/* The DIE for C++14 'auto' in a function return type. */
271static GTY(()) dw_die_ref auto_die;
272
273/* The DIE for C++14 'decltype(auto)' in a function return type. */
274static GTY(()) dw_die_ref decltype_auto_die;
275
276/* Forward declarations for functions defined in this file. */
277
278static void output_call_frame_info (int);
279static void dwarf2out_note_section_used (void);
280
281/* Personality decl of current unit. Used only when assembler does not support
282 personality CFI. */
283static GTY(()) rtx current_unit_personality;
284
285/* .debug_rnglists next index. */
286static unsigned int rnglist_idx;
287
288/* Data and reference forms for relocatable data. */
289#define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
290#define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
291
292#ifndef DEBUG_FRAME_SECTION
293#define DEBUG_FRAME_SECTION ".debug_frame"
294#endif
295
296#ifndef FUNC_BEGIN_LABEL
297#define FUNC_BEGIN_LABEL "LFB"
298#endif
299
300#ifndef FUNC_SECOND_SECT_LABEL
301#define FUNC_SECOND_SECT_LABEL "LFSB"
302#endif
303
304#ifndef FUNC_END_LABEL
305#define FUNC_END_LABEL "LFE"
306#endif
307
308#ifndef PROLOGUE_END_LABEL
309#define PROLOGUE_END_LABEL "LPE"
310#endif
311
312#ifndef EPILOGUE_BEGIN_LABEL
313#define EPILOGUE_BEGIN_LABEL "LEB"
314#endif
315
316#ifndef FRAME_BEGIN_LABEL
317#define FRAME_BEGIN_LABEL "Lframe"
318#endif
319#define CIE_AFTER_SIZE_LABEL "LSCIE"
320#define CIE_END_LABEL "LECIE"
321#define FDE_LABEL "LSFDE"
322#define FDE_AFTER_SIZE_LABEL "LASFDE"
323#define FDE_END_LABEL "LEFDE"
324#define LINE_NUMBER_BEGIN_LABEL "LSLT"
325#define LINE_NUMBER_END_LABEL "LELT"
326#define LN_PROLOG_AS_LABEL "LASLTP"
327#define LN_PROLOG_END_LABEL "LELTP"
328#define DIE_LABEL_PREFIX "DW"
329
330/* Match the base name of a file to the base name of a compilation unit. */
331
332static int
333matches_main_base (const char *path)
334{
335 /* Cache the last query. */
336 static const char *last_path = NULL;
337 static int last_match = 0;
338 if (path != last_path)
339 {
340 const char *base;
341 int length = base_of_path (path, &base);
342 last_path = path;
343 last_match = (length == main_input_baselength
344 && memcmp (base, main_input_basename, length) == 0);
345 }
346 return last_match;
347}
348
349#ifdef DEBUG_DEBUG_STRUCT
350
351static int
352dump_struct_debug (tree type, enum debug_info_usage usage,
353 enum debug_struct_file criterion, int generic,
354 int matches, int result)
355{
356 /* Find the type name. */
357 tree type_decl = TYPE_STUB_DECL (type);
358 tree t = type_decl;
359 const char *name = 0;
360 if (TREE_CODE (t) == TYPE_DECL)
361 t = DECL_NAME (t);
362 if (t)
363 name = IDENTIFIER_POINTER (t);
364
365 fprintf (stderr, " struct %d %s %s %s %s %d %p %s\n",
366 criterion,
367 DECL_IN_SYSTEM_HEADER (type_decl) ? "sys" : "usr",
368 matches ? "bas" : "hdr",
369 generic ? "gen" : "ord",
370 usage == DINFO_USAGE_DFN ? ";" :
371 usage == DINFO_USAGE_DIR_USE ? "." : "*",
372 result,
373 (void*) type_decl, name);
374 return result;
375}
376#define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
377 dump_struct_debug (type, usage, criterion, generic, matches, result)
378
379#else
380
381#define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
382 (result)
383
384#endif
385
386/* Get the number of HOST_WIDE_INTs needed to represent the precision
387 of the number. Some constants have a large uniform precision, so
388 we get the precision needed for the actual value of the number. */
389
390static unsigned int
391get_full_len (const wide_int &op)
392{
393 int prec = wi::min_precision (op, UNSIGNED);
394 return ((prec + HOST_BITS_PER_WIDE_INT - 1)
395 / HOST_BITS_PER_WIDE_INT);
396}
397
398static bool
399should_emit_struct_debug (tree type, enum debug_info_usage usage)
400{
401 enum debug_struct_file criterion;
402 tree type_decl;
403 bool generic = lang_hooks.types.generic_p (type);
404
405 if (generic)
406 criterion = debug_struct_generic[usage];
407 else
408 criterion = debug_struct_ordinary[usage];
409
410 if (criterion == DINFO_STRUCT_FILE_NONE)
411 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
412 if (criterion == DINFO_STRUCT_FILE_ANY)
413 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
414
415 type_decl = TYPE_STUB_DECL (TYPE_MAIN_VARIANT (type));
416
417 if (type_decl != NULL)
418 {
419 if (criterion == DINFO_STRUCT_FILE_SYS && DECL_IN_SYSTEM_HEADER (type_decl))
420 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
421
422 if (matches_main_base (DECL_SOURCE_FILE (type_decl)))
423 return DUMP_GSTRUCT (type, usage, criterion, generic, true, true);
424 }
425
426 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
427}
428
429/* Switch [BACK] to eh_frame_section. If we don't have an eh_frame_section,
430 switch to the data section instead, and write out a synthetic start label
431 for collect2 the first time around. */
432
433static void
434switch_to_eh_frame_section (bool back ATTRIBUTE_UNUSED)
435{
436 if (eh_frame_section == 0)
437 {
438 int flags;
439
440 if (EH_TABLES_CAN_BE_READ_ONLY)
441 {
442 int fde_encoding;
443 int per_encoding;
444 int lsda_encoding;
445
446 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
447 /*global=*/0);
448 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
449 /*global=*/1);
450 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
451 /*global=*/0);
452 flags = ((! flag_pic
453 || ((fde_encoding & 0x70) != DW_EH_PE_absptr
454 && (fde_encoding & 0x70) != DW_EH_PE_aligned
455 && (per_encoding & 0x70) != DW_EH_PE_absptr
456 && (per_encoding & 0x70) != DW_EH_PE_aligned
457 && (lsda_encoding & 0x70) != DW_EH_PE_absptr
458 && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
459 ? 0 : SECTION_WRITE);
460 }
461 else
462 flags = SECTION_WRITE;
463
464#ifdef EH_FRAME_SECTION_NAME
465 eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
466#else
467 eh_frame_section = ((flags == SECTION_WRITE)
468 ? data_section : readonly_data_section);
469#endif /* EH_FRAME_SECTION_NAME */
470 }
471
472 switch_to_section (eh_frame_section);
473
474#ifdef EH_FRAME_THROUGH_COLLECT2
475 /* We have no special eh_frame section. Emit special labels to guide
476 collect2. */
477 if (!back)
478 {
479 tree label = get_file_function_name ("F");
480 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
481 targetm.asm_out.globalize_label (asm_out_file,
482 IDENTIFIER_POINTER (label));
483 ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
484 }
485#endif
486}
487
488/* Switch [BACK] to the eh or debug frame table section, depending on
489 FOR_EH. */
490
491static void
492switch_to_frame_table_section (int for_eh, bool back)
493{
494 if (for_eh)
495 switch_to_eh_frame_section (back);
496 else
497 {
498 if (!debug_frame_section)
499 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
500 SECTION_DEBUG, NULL);
501 switch_to_section (debug_frame_section);
502 }
503}
504
505/* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used. */
506
507enum dw_cfi_oprnd_type
508dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
509{
510 switch (cfi)
511 {
512 case DW_CFA_nop:
513 case DW_CFA_GNU_window_save:
514 case DW_CFA_remember_state:
515 case DW_CFA_restore_state:
516 return dw_cfi_oprnd_unused;
517
518 case DW_CFA_set_loc:
519 case DW_CFA_advance_loc1:
520 case DW_CFA_advance_loc2:
521 case DW_CFA_advance_loc4:
522 case DW_CFA_MIPS_advance_loc8:
523 return dw_cfi_oprnd_addr;
524
525 case DW_CFA_offset:
526 case DW_CFA_offset_extended:
527 case DW_CFA_def_cfa:
528 case DW_CFA_offset_extended_sf:
529 case DW_CFA_def_cfa_sf:
530 case DW_CFA_restore:
531 case DW_CFA_restore_extended:
532 case DW_CFA_undefined:
533 case DW_CFA_same_value:
534 case DW_CFA_def_cfa_register:
535 case DW_CFA_register:
536 case DW_CFA_expression:
537 case DW_CFA_val_expression:
538 return dw_cfi_oprnd_reg_num;
539
540 case DW_CFA_def_cfa_offset:
541 case DW_CFA_GNU_args_size:
542 case DW_CFA_def_cfa_offset_sf:
543 return dw_cfi_oprnd_offset;
544
545 case DW_CFA_def_cfa_expression:
546 return dw_cfi_oprnd_loc;
547
548 default:
549 gcc_unreachable ();
550 }
551}
552
553/* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used. */
554
555enum dw_cfi_oprnd_type
556dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
557{
558 switch (cfi)
559 {
560 case DW_CFA_def_cfa:
561 case DW_CFA_def_cfa_sf:
562 case DW_CFA_offset:
563 case DW_CFA_offset_extended_sf:
564 case DW_CFA_offset_extended:
565 return dw_cfi_oprnd_offset;
566
567 case DW_CFA_register:
568 return dw_cfi_oprnd_reg_num;
569
570 case DW_CFA_expression:
571 case DW_CFA_val_expression:
572 return dw_cfi_oprnd_loc;
573
574 default:
575 return dw_cfi_oprnd_unused;
576 }
577}
578
579/* Output one FDE. */
580
581static void
582output_fde (dw_fde_ref fde, bool for_eh, bool second,
583 char *section_start_label, int fde_encoding, char *augmentation,
584 bool any_lsda_needed, int lsda_encoding)
585{
586 const char *begin, *end;
587 static unsigned int j;
588 char l1[MAX_ARTIFICIAL_LABEL_BYTES], l2[MAX_ARTIFICIAL_LABEL_BYTES];
589
590 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, for_eh,
591 /* empty */ 0);
592 targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
593 for_eh + j);
594 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
595 ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
596 if (!XCOFF_DEBUGGING_INFO || for_eh)
597 {
598 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
599 dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
600 " indicating 64-bit DWARF extension");
601 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
602 "FDE Length");
603 }
604 ASM_OUTPUT_LABEL (asm_out_file, l1);
605
606 if (for_eh)
607 dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
608 else
609 dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
610 debug_frame_section, "FDE CIE offset");
611
612 begin = second ? fde->dw_fde_second_begin : fde->dw_fde_begin;
613 end = second ? fde->dw_fde_second_end : fde->dw_fde_end;
614
615 if (for_eh)
616 {
617 rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
618 SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
619 dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
620 "FDE initial location");
621 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
622 end, begin, "FDE address range");
623 }
624 else
625 {
626 dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
627 dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
628 }
629
630 if (augmentation[0])
631 {
632 if (any_lsda_needed)
633 {
634 int size = size_of_encoded_value (lsda_encoding);
635
636 if (lsda_encoding == DW_EH_PE_aligned)
637 {
638 int offset = ( 4 /* Length */
639 + 4 /* CIE offset */
640 + 2 * size_of_encoded_value (fde_encoding)
641 + 1 /* Augmentation size */ );
642 int pad = -offset & (PTR_SIZE - 1);
643
644 size += pad;
645 gcc_assert (size_of_uleb128 (size) == 1);
646 }
647
648 dw2_asm_output_data_uleb128 (size, "Augmentation size");
649
650 if (fde->uses_eh_lsda)
651 {
652 ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
653 fde->funcdef_number);
654 dw2_asm_output_encoded_addr_rtx (lsda_encoding,
655 gen_rtx_SYMBOL_REF (Pmode, l1),
656 false,
657 "Language Specific Data Area");
658 }
659 else
660 {
661 if (lsda_encoding == DW_EH_PE_aligned)
662 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
663 dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
664 "Language Specific Data Area (none)");
665 }
666 }
667 else
668 dw2_asm_output_data_uleb128 (0, "Augmentation size");
669 }
670
671 /* Loop through the Call Frame Instructions associated with this FDE. */
672 fde->dw_fde_current_label = begin;
673 {
674 size_t from, until, i;
675
676 from = 0;
677 until = vec_safe_length (fde->dw_fde_cfi);
678
679 if (fde->dw_fde_second_begin == NULL)
680 ;
681 else if (!second)
682 until = fde->dw_fde_switch_cfi_index;
683 else
684 from = fde->dw_fde_switch_cfi_index;
685
686 for (i = from; i < until; i++)
687 output_cfi ((*fde->dw_fde_cfi)[i], fde, for_eh);
688 }
689
690 /* If we are to emit a ref/link from function bodies to their frame tables,
691 do it now. This is typically performed to make sure that tables
692 associated with functions are dragged with them and not discarded in
693 garbage collecting links. We need to do this on a per function basis to
694 cope with -ffunction-sections. */
695
696#ifdef ASM_OUTPUT_DWARF_TABLE_REF
697 /* Switch to the function section, emit the ref to the tables, and
698 switch *back* into the table section. */
699 switch_to_section (function_section (fde->decl));
700 ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
701 switch_to_frame_table_section (for_eh, true);
702#endif
703
704 /* Pad the FDE out to an address sized boundary. */
705 ASM_OUTPUT_ALIGN (asm_out_file,
706 floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
707 ASM_OUTPUT_LABEL (asm_out_file, l2);
708
709 j += 2;
710}
711
712/* Return true if frame description entry FDE is needed for EH. */
713
714static bool
715fde_needed_for_eh_p (dw_fde_ref fde)
716{
717 if (flag_asynchronous_unwind_tables)
718 return true;
719
720 if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde->decl))
721 return true;
722
723 if (fde->uses_eh_lsda)
724 return true;
725
726 /* If exceptions are enabled, we have collected nothrow info. */
727 if (flag_exceptions && (fde->all_throwers_are_sibcalls || fde->nothrow))
728 return false;
729
730 return true;
731}
732
733/* Output the call frame information used to record information
734 that relates to calculating the frame pointer, and records the
735 location of saved registers. */
736
737static void
738output_call_frame_info (int for_eh)
739{
740 unsigned int i;
741 dw_fde_ref fde;
742 dw_cfi_ref cfi;
743 char l1[MAX_ARTIFICIAL_LABEL_BYTES], l2[MAX_ARTIFICIAL_LABEL_BYTES];
744 char section_start_label[MAX_ARTIFICIAL_LABEL_BYTES];
745 bool any_lsda_needed = false;
746 char augmentation[6];
747 int augmentation_size;
748 int fde_encoding = DW_EH_PE_absptr;
749 int per_encoding = DW_EH_PE_absptr;
750 int lsda_encoding = DW_EH_PE_absptr;
751 int return_reg;
752 rtx personality = NULL;
753 int dw_cie_version;
754
755 /* Don't emit a CIE if there won't be any FDEs. */
756 if (!fde_vec)
757 return;
758
759 /* Nothing to do if the assembler's doing it all. */
760 if (dwarf2out_do_cfi_asm ())
761 return;
762
763 /* If we don't have any functions we'll want to unwind out of, don't emit
764 any EH unwind information. If we make FDEs linkonce, we may have to
765 emit an empty label for an FDE that wouldn't otherwise be emitted. We
766 want to avoid having an FDE kept around when the function it refers to
767 is discarded. Example where this matters: a primary function template
768 in C++ requires EH information, an explicit specialization doesn't. */
769 if (for_eh)
770 {
771 bool any_eh_needed = false;
772
773 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
774 {
775 if (fde->uses_eh_lsda)
776 any_eh_needed = any_lsda_needed = true;
777 else if (fde_needed_for_eh_p (fde))
778 any_eh_needed = true;
779 else if (TARGET_USES_WEAK_UNWIND_INFO)
780 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, 1, 1);
781 }
782
783 if (!any_eh_needed)
784 return;
785 }
786
787 /* We're going to be generating comments, so turn on app. */
788 if (flag_debug_asm)
789 app_enable ();
790
791 /* Switch to the proper frame section, first time. */
792 switch_to_frame_table_section (for_eh, false);
793
794 ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
795 ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
796
797 /* Output the CIE. */
798 ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
799 ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
800 if (!XCOFF_DEBUGGING_INFO || for_eh)
801 {
802 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
803 dw2_asm_output_data (4, 0xffffffff,
804 "Initial length escape value indicating 64-bit DWARF extension");
805 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
806 "Length of Common Information Entry");
807 }
808 ASM_OUTPUT_LABEL (asm_out_file, l1);
809
810 /* Now that the CIE pointer is PC-relative for EH,
811 use 0 to identify the CIE. */
812 dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
813 (for_eh ? 0 : DWARF_CIE_ID),
814 "CIE Identifier Tag");
815
816 /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
817 use CIE version 1, unless that would produce incorrect results
818 due to overflowing the return register column. */
819 return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
820 dw_cie_version = 1;
821 if (return_reg >= 256 || dwarf_version > 2)
822 dw_cie_version = 3;
823 dw2_asm_output_data (1, dw_cie_version, "CIE Version");
824
825 augmentation[0] = 0;
826 augmentation_size = 0;
827
828 personality = current_unit_personality;
829 if (for_eh)
830 {
831 char *p;
832
833 /* Augmentation:
834 z Indicates that a uleb128 is present to size the
835 augmentation section.
836 L Indicates the encoding (and thus presence) of
837 an LSDA pointer in the FDE augmentation.
838 R Indicates a non-default pointer encoding for
839 FDE code pointers.
840 P Indicates the presence of an encoding + language
841 personality routine in the CIE augmentation. */
842
843 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
844 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
845 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
846
847 p = augmentation + 1;
848 if (personality)
849 {
850 *p++ = 'P';
851 augmentation_size += 1 + size_of_encoded_value (per_encoding);
852 assemble_external_libcall (personality);
853 }
854 if (any_lsda_needed)
855 {
856 *p++ = 'L';
857 augmentation_size += 1;
858 }
859 if (fde_encoding != DW_EH_PE_absptr)
860 {
861 *p++ = 'R';
862 augmentation_size += 1;
863 }
864 if (p > augmentation + 1)
865 {
866 augmentation[0] = 'z';
867 *p = '\0';
868 }
869
870 /* Ug. Some platforms can't do unaligned dynamic relocations at all. */
871 if (personality && per_encoding == DW_EH_PE_aligned)
872 {
873 int offset = ( 4 /* Length */
874 + 4 /* CIE Id */
875 + 1 /* CIE version */
876 + strlen (augmentation) + 1 /* Augmentation */
877 + size_of_uleb128 (1) /* Code alignment */
878 + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
879 + 1 /* RA column */
880 + 1 /* Augmentation size */
881 + 1 /* Personality encoding */ );
882 int pad = -offset & (PTR_SIZE - 1);
883
884 augmentation_size += pad;
885
886 /* Augmentations should be small, so there's scarce need to
887 iterate for a solution. Die if we exceed one uleb128 byte. */
888 gcc_assert (size_of_uleb128 (augmentation_size) == 1);
889 }
890 }
891
892 dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
893 if (dw_cie_version >= 4)
894 {
895 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "CIE Address Size");
896 dw2_asm_output_data (1, 0, "CIE Segment Size");
897 }
898 dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
899 dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
900 "CIE Data Alignment Factor");
901
902 if (dw_cie_version == 1)
903 dw2_asm_output_data (1, return_reg, "CIE RA Column");
904 else
905 dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
906
907 if (augmentation[0])
908 {
909 dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
910 if (personality)
911 {
912 dw2_asm_output_data (1, per_encoding, "Personality (%s)",
913 eh_data_format_name (per_encoding));
914 dw2_asm_output_encoded_addr_rtx (per_encoding,
915 personality,
916 true, NULL);
917 }
918
919 if (any_lsda_needed)
920 dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
921 eh_data_format_name (lsda_encoding));
922
923 if (fde_encoding != DW_EH_PE_absptr)
924 dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
925 eh_data_format_name (fde_encoding));
926 }
927
928 FOR_EACH_VEC_ELT (*cie_cfi_vec, i, cfi)
929 output_cfi (cfi, NULL, for_eh);
930
931 /* Pad the CIE out to an address sized boundary. */
932 ASM_OUTPUT_ALIGN (asm_out_file,
933 floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
934 ASM_OUTPUT_LABEL (asm_out_file, l2);
935
936 /* Loop through all of the FDE's. */
937 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
938 {
939 unsigned int k;
940
941 /* Don't emit EH unwind info for leaf functions that don't need it. */
942 if (for_eh && !fde_needed_for_eh_p (fde))
943 continue;
944
945 for (k = 0; k < (fde->dw_fde_second_begin ? 2 : 1); k++)
946 output_fde (fde, for_eh, k, section_start_label, fde_encoding,
947 augmentation, any_lsda_needed, lsda_encoding);
948 }
949
950 if (for_eh && targetm.terminate_dw2_eh_frame_info)
951 dw2_asm_output_data (4, 0, "End of Table");
952
953 /* Turn off app to make assembly quicker. */
954 if (flag_debug_asm)
955 app_disable ();
956}
957
958/* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed. */
959
960static void
961dwarf2out_do_cfi_startproc (bool second)
962{
963 int enc;
964 rtx ref;
965 rtx personality = get_personality_function (current_function_decl);
966
967 fprintf (asm_out_file, "\t.cfi_startproc\n");
968
969 if (personality)
970 {
971 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
972 ref = personality;
973
974 /* ??? The GAS support isn't entirely consistent. We have to
975 handle indirect support ourselves, but PC-relative is done
976 in the assembler. Further, the assembler can't handle any
977 of the weirder relocation types. */
978 if (enc & DW_EH_PE_indirect)
979 ref = dw2_force_const_mem (ref, true);
980
981 fprintf (asm_out_file, "\t.cfi_personality %#x,", enc);
982 output_addr_const (asm_out_file, ref);
983 fputc ('\n', asm_out_file);
984 }
985
986 if (crtl->uses_eh_lsda)
987 {
988 char lab[MAX_ARTIFICIAL_LABEL_BYTES];
989
990 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
991 ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
992 current_function_funcdef_no);
993 ref = gen_rtx_SYMBOL_REF (Pmode, lab);
994 SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
995
996 if (enc & DW_EH_PE_indirect)
997 ref = dw2_force_const_mem (ref, true);
998
999 fprintf (asm_out_file, "\t.cfi_lsda %#x,", enc);
1000 output_addr_const (asm_out_file, ref);
1001 fputc ('\n', asm_out_file);
1002 }
1003}
1004
1005/* Allocate CURRENT_FDE. Immediately initialize all we can, noting that
1006 this allocation may be done before pass_final. */
1007
1008dw_fde_ref
1009dwarf2out_alloc_current_fde (void)
1010{
1011 dw_fde_ref fde;
1012
1013 fde = ggc_cleared_alloc<dw_fde_node> ();
1014 fde->decl = current_function_decl;
1015 fde->funcdef_number = current_function_funcdef_no;
1016 fde->fde_index = vec_safe_length (fde_vec);
1017 fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
1018 fde->uses_eh_lsda = crtl->uses_eh_lsda;
1019 fde->nothrow = crtl->nothrow;
1020 fde->drap_reg = INVALID_REGNUM;
1021 fde->vdrap_reg = INVALID_REGNUM;
1022
1023 /* Record the FDE associated with this function. */
1024 cfun->fde = fde;
1025 vec_safe_push (fde_vec, fde);
1026
1027 return fde;
1028}
1029
1030/* Output a marker (i.e. a label) for the beginning of a function, before
1031 the prologue. */
1032
1033void
1034dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
1035 unsigned int column ATTRIBUTE_UNUSED,
1036 const char *file ATTRIBUTE_UNUSED)
1037{
1038 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1039 char * dup_label;
1040 dw_fde_ref fde;
1041 section *fnsec;
1042 bool do_frame;
1043
1044 current_function_func_begin_label = NULL;
1045
1046 do_frame = dwarf2out_do_frame ();
1047
1048 /* ??? current_function_func_begin_label is also used by except.c for
1049 call-site information. We must emit this label if it might be used. */
1050 if (!do_frame
1051 && (!flag_exceptions
1052 || targetm_common.except_unwind_info (&global_options) == UI_SJLJ))
1053 return;
1054
1055 fnsec = function_section (current_function_decl);
1056 switch_to_section (fnsec);
1057 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
1058 current_function_funcdef_no);
1059 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
1060 current_function_funcdef_no);
1061 dup_label = xstrdup (label);
1062 current_function_func_begin_label = dup_label;
1063
1064 /* We can elide the fde allocation if we're not emitting debug info. */
1065 if (!do_frame)
1066 return;
1067
1068 /* Cater to the various TARGET_ASM_OUTPUT_MI_THUNK implementations that
1069 emit insns as rtx but bypass the bulk of rest_of_compilation, which
1070 would include pass_dwarf2_frame. If we've not created the FDE yet,
1071 do so now. */
1072 fde = cfun->fde;
1073 if (fde == NULL)
1074 fde = dwarf2out_alloc_current_fde ();
1075
1076 /* Initialize the bits of CURRENT_FDE that were not available earlier. */
1077 fde->dw_fde_begin = dup_label;
1078 fde->dw_fde_current_label = dup_label;
1079 fde->in_std_section = (fnsec == text_section
1080 || (cold_text_section && fnsec == cold_text_section));
1081
1082 /* We only want to output line number information for the genuine dwarf2
1083 prologue case, not the eh frame case. */
1084#ifdef DWARF2_DEBUGGING_INFO
1085 if (file)
1086 dwarf2out_source_line (line, column, file, 0, true);
1087#endif
1088
1089 if (dwarf2out_do_cfi_asm ())
1090 dwarf2out_do_cfi_startproc (false);
1091 else
1092 {
1093 rtx personality = get_personality_function (current_function_decl);
1094 if (!current_unit_personality)
1095 current_unit_personality = personality;
1096
1097 /* We cannot keep a current personality per function as without CFI
1098 asm, at the point where we emit the CFI data, there is no current
1099 function anymore. */
1100 if (personality && current_unit_personality != personality)
1101 sorry ("multiple EH personalities are supported only with assemblers "
1102 "supporting .cfi_personality directive");
1103 }
1104}
1105
1106/* Output a marker (i.e. a label) for the end of the generated code
1107 for a function prologue. This gets called *after* the prologue code has
1108 been generated. */
1109
1110void
1111dwarf2out_vms_end_prologue (unsigned int line ATTRIBUTE_UNUSED,
1112 const char *file ATTRIBUTE_UNUSED)
1113{
1114 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1115
1116 /* Output a label to mark the endpoint of the code generated for this
1117 function. */
1118 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
1119 current_function_funcdef_no);
1120 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, PROLOGUE_END_LABEL,
1121 current_function_funcdef_no);
1122 cfun->fde->dw_fde_vms_end_prologue = xstrdup (label);
1123}
1124
1125/* Output a marker (i.e. a label) for the beginning of the generated code
1126 for a function epilogue. This gets called *before* the prologue code has
1127 been generated. */
1128
1129void
1130dwarf2out_vms_begin_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1131 const char *file ATTRIBUTE_UNUSED)
1132{
1133 dw_fde_ref fde = cfun->fde;
1134 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1135
1136 if (fde->dw_fde_vms_begin_epilogue)
1137 return;
1138
1139 /* Output a label to mark the endpoint of the code generated for this
1140 function. */
1141 ASM_GENERATE_INTERNAL_LABEL (label, EPILOGUE_BEGIN_LABEL,
1142 current_function_funcdef_no);
1143 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, EPILOGUE_BEGIN_LABEL,
1144 current_function_funcdef_no);
1145 fde->dw_fde_vms_begin_epilogue = xstrdup (label);
1146}
1147
1148/* Output a marker (i.e. a label) for the absolute end of the generated code
1149 for a function definition. This gets called *after* the epilogue code has
1150 been generated. */
1151
1152void
1153dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1154 const char *file ATTRIBUTE_UNUSED)
1155{
1156 dw_fde_ref fde;
1157 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1158
1159 last_var_location_insn = NULL;
1160 cached_next_real_insn = NULL;
1161
1162 if (dwarf2out_do_cfi_asm ())
1163 fprintf (asm_out_file, "\t.cfi_endproc\n");
1164
1165 /* Output a label to mark the endpoint of the code generated for this
1166 function. */
1167 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
1168 current_function_funcdef_no);
1169 ASM_OUTPUT_LABEL (asm_out_file, label);
1170 fde = cfun->fde;
1171 gcc_assert (fde != NULL);
1172 if (fde->dw_fde_second_begin == NULL)
1173 fde->dw_fde_end = xstrdup (label);
1174}
1175
1176void
1177dwarf2out_frame_finish (void)
1178{
1179 /* Output call frame information. */
1180 if (targetm.debug_unwind_info () == UI_DWARF2)
1181 output_call_frame_info (0);
1182
1183 /* Output another copy for the unwinder. */
1184 if ((flag_unwind_tables || flag_exceptions)
1185 && targetm_common.except_unwind_info (&global_options) == UI_DWARF2)
1186 output_call_frame_info (1);
1187}
1188
1189/* Note that the current function section is being used for code. */
1190
1191static void
1192dwarf2out_note_section_used (void)
1193{
1194 section *sec = current_function_section ();
1195 if (sec == text_section)
1196 text_section_used = true;
1197 else if (sec == cold_text_section)
1198 cold_text_section_used = true;
1199}
1200
1201static void var_location_switch_text_section (void);
1202static void set_cur_line_info_table (section *);
1203
1204void
1205dwarf2out_switch_text_section (void)
1206{
1207 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1208 section *sect;
1209 dw_fde_ref fde = cfun->fde;
1210
1211 gcc_assert (cfun && fde && fde->dw_fde_second_begin == NULL);
1212
1213 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_SECOND_SECT_LABEL,
1214 current_function_funcdef_no);
1215
1216 fde->dw_fde_second_begin = ggc_strdup (label);
1217 if (!in_cold_section_p)
1218 {
1219 fde->dw_fde_end = crtl->subsections.cold_section_end_label;
1220 fde->dw_fde_second_end = crtl->subsections.hot_section_end_label;
1221 }
1222 else
1223 {
1224 fde->dw_fde_end = crtl->subsections.hot_section_end_label;
1225 fde->dw_fde_second_end = crtl->subsections.cold_section_end_label;
1226 }
1227 have_multiple_function_sections = true;
1228
1229 /* There is no need to mark used sections when not debugging. */
1230 if (cold_text_section != NULL)
1231 dwarf2out_note_section_used ();
1232
1233 if (dwarf2out_do_cfi_asm ())
1234 fprintf (asm_out_file, "\t.cfi_endproc\n");
1235
1236 /* Now do the real section switch. */
1237 sect = current_function_section ();
1238 switch_to_section (sect);
1239
1240 fde->second_in_std_section
1241 = (sect == text_section
1242 || (cold_text_section && sect == cold_text_section));
1243
1244 if (dwarf2out_do_cfi_asm ())
1245 dwarf2out_do_cfi_startproc (true);
1246
1247 var_location_switch_text_section ();
1248
1249 if (cold_text_section != NULL)
1250 set_cur_line_info_table (sect);
1251}
1252
1253/* And now, the subset of the debugging information support code necessary
1254 for emitting location expressions. */
1255
1256/* Data about a single source file. */
1257struct GTY((for_user)) dwarf_file_data {
1258 const char * filename;
1259 int emitted_number;
1260};
1261
1262/* Describe an entry into the .debug_addr section. */
1263
1264enum ate_kind {
1265 ate_kind_rtx,
1266 ate_kind_rtx_dtprel,
1267 ate_kind_label
1268};
1269
1270struct GTY((for_user)) addr_table_entry {
1271 enum ate_kind kind;
1272 unsigned int refcount;
1273 unsigned int index;
1274 union addr_table_entry_struct_union
1275 {
1276 rtx GTY ((tag ("0"))) rtl;
1277 char * GTY ((tag ("1"))) label;
1278 }
1279 GTY ((desc ("%1.kind"))) addr;
1280};
1281
1282/* Location lists are ranges + location descriptions for that range,
1283 so you can track variables that are in different places over
1284 their entire life. */
1285typedef struct GTY(()) dw_loc_list_struct {
1286 dw_loc_list_ref dw_loc_next;
1287 const char *begin; /* Label and addr_entry for start of range */
1288 addr_table_entry *begin_entry;
1289 const char *end; /* Label for end of range */
1290 char *ll_symbol; /* Label for beginning of location list.
1291 Only on head of list */
1292 const char *section; /* Section this loclist is relative to */
1293 dw_loc_descr_ref expr;
1294 hashval_t hash;
1295 /* True if all addresses in this and subsequent lists are known to be
1296 resolved. */
1297 bool resolved_addr;
1298 /* True if this list has been replaced by dw_loc_next. */
1299 bool replaced;
1300 /* True if it has been emitted into .debug_loc* / .debug_loclists*
1301 section. */
1302 unsigned char emitted : 1;
1303 /* True if hash field is index rather than hash value. */
1304 unsigned char num_assigned : 1;
1305 /* True if .debug_loclists.dwo offset has been emitted for it already. */
1306 unsigned char offset_emitted : 1;
1307 /* True if note_variable_value_in_expr has been called on it. */
1308 unsigned char noted_variable_value : 1;
1309 /* True if the range should be emitted even if begin and end
1310 are the same. */
1311 bool force;
1312} dw_loc_list_node;
1313
1314static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
1315static dw_loc_descr_ref uint_loc_descriptor (unsigned HOST_WIDE_INT);
1316
1317/* Convert a DWARF stack opcode into its string name. */
1318
1319static const char *
1320dwarf_stack_op_name (unsigned int op)
1321{
1322 const char *name = get_DW_OP_name (op);
1323
1324 if (name != NULL)
1325 return name;
1326
1327 return "OP_<unknown>";
1328}
1329
1330/* Return a pointer to a newly allocated location description. Location
1331 descriptions are simple expression terms that can be strung
1332 together to form more complicated location (address) descriptions. */
1333
1334static inline dw_loc_descr_ref
1335new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
1336 unsigned HOST_WIDE_INT oprnd2)
1337{
1338 dw_loc_descr_ref descr = ggc_cleared_alloc<dw_loc_descr_node> ();
1339
1340 descr->dw_loc_opc = op;
1341 descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
1342 descr->dw_loc_oprnd1.val_entry = NULL;
1343 descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
1344 descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
1345 descr->dw_loc_oprnd2.val_entry = NULL;
1346 descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
1347
1348 return descr;
1349}
1350
1351/* Return a pointer to a newly allocated location description for
1352 REG and OFFSET. */
1353
1354static inline dw_loc_descr_ref
1355new_reg_loc_descr (unsigned int reg, unsigned HOST_WIDE_INT offset)
1356{
1357 if (reg <= 31)
1358 return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
1359 offset, 0);
1360 else
1361 return new_loc_descr (DW_OP_bregx, reg, offset);
1362}
1363
1364/* Add a location description term to a location description expression. */
1365
1366static inline void
1367add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
1368{
1369 dw_loc_descr_ref *d;
1370
1371 /* Find the end of the chain. */
1372 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
1373 ;
1374
1375 *d = descr;
1376}
1377
1378/* Compare two location operands for exact equality. */
1379
1380static bool
1381dw_val_equal_p (dw_val_node *a, dw_val_node *b)
1382{
1383 if (a->val_class != b->val_class)
1384 return false;
1385 switch (a->val_class)
1386 {
1387 case dw_val_class_none:
1388 return true;
1389 case dw_val_class_addr:
1390 return rtx_equal_p (a->v.val_addr, b->v.val_addr);
1391
1392 case dw_val_class_offset:
1393 case dw_val_class_unsigned_const:
1394 case dw_val_class_const:
1395 case dw_val_class_unsigned_const_implicit:
1396 case dw_val_class_const_implicit:
1397 case dw_val_class_range_list:
1398 /* These are all HOST_WIDE_INT, signed or unsigned. */
1399 return a->v.val_unsigned == b->v.val_unsigned;
1400
1401 case dw_val_class_loc:
1402 return a->v.val_loc == b->v.val_loc;
1403 case dw_val_class_loc_list:
1404 return a->v.val_loc_list == b->v.val_loc_list;
1405 case dw_val_class_die_ref:
1406 return a->v.val_die_ref.die == b->v.val_die_ref.die;
1407 case dw_val_class_fde_ref:
1408 return a->v.val_fde_index == b->v.val_fde_index;
1409 case dw_val_class_lbl_id:
1410 case dw_val_class_lineptr:
1411 case dw_val_class_macptr:
1412 case dw_val_class_loclistsptr:
1413 case dw_val_class_high_pc:
1414 return strcmp (a->v.val_lbl_id, b->v.val_lbl_id) == 0;
1415 case dw_val_class_str:
1416 return a->v.val_str == b->v.val_str;
1417 case dw_val_class_flag:
1418 return a->v.val_flag == b->v.val_flag;
1419 case dw_val_class_file:
1420 case dw_val_class_file_implicit:
1421 return a->v.val_file == b->v.val_file;
1422 case dw_val_class_decl_ref:
1423 return a->v.val_decl_ref == b->v.val_decl_ref;
1424
1425 case dw_val_class_const_double:
1426 return (a->v.val_double.high == b->v.val_double.high
1427 && a->v.val_double.low == b->v.val_double.low);
1428
1429 case dw_val_class_wide_int:
1430 return *a->v.val_wide == *b->v.val_wide;
1431
1432 case dw_val_class_vec:
1433 {
1434 size_t a_len = a->v.val_vec.elt_size * a->v.val_vec.length;
1435 size_t b_len = b->v.val_vec.elt_size * b->v.val_vec.length;
1436
1437 return (a_len == b_len
1438 && !memcmp (a->v.val_vec.array, b->v.val_vec.array, a_len));
1439 }
1440
1441 case dw_val_class_data8:
1442 return memcmp (a->v.val_data8, b->v.val_data8, 8) == 0;
1443
1444 case dw_val_class_vms_delta:
1445 return (!strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1)
1446 && !strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1));
1447
1448 case dw_val_class_discr_value:
1449 return (a->v.val_discr_value.pos == b->v.val_discr_value.pos
1450 && a->v.val_discr_value.v.uval == b->v.val_discr_value.v.uval);
1451 case dw_val_class_discr_list:
1452 /* It makes no sense comparing two discriminant value lists. */
1453 return false;
1454 }
1455 gcc_unreachable ();
1456}
1457
1458/* Compare two location atoms for exact equality. */
1459
1460static bool
1461loc_descr_equal_p_1 (dw_loc_descr_ref a, dw_loc_descr_ref b)
1462{
1463 if (a->dw_loc_opc != b->dw_loc_opc)
1464 return false;
1465
1466 /* ??? This is only ever set for DW_OP_constNu, for N equal to the
1467 address size, but since we always allocate cleared storage it
1468 should be zero for other types of locations. */
1469 if (a->dtprel != b->dtprel)
1470 return false;
1471
1472 return (dw_val_equal_p (&a->dw_loc_oprnd1, &b->dw_loc_oprnd1)
1473 && dw_val_equal_p (&a->dw_loc_oprnd2, &b->dw_loc_oprnd2));
1474}
1475
1476/* Compare two complete location expressions for exact equality. */
1477
1478bool
1479loc_descr_equal_p (dw_loc_descr_ref a, dw_loc_descr_ref b)
1480{
1481 while (1)
1482 {
1483 if (a == b)
1484 return true;
1485 if (a == NULL || b == NULL)
1486 return false;
1487 if (!loc_descr_equal_p_1 (a, b))
1488 return false;
1489
1490 a = a->dw_loc_next;
1491 b = b->dw_loc_next;
1492 }
1493}
1494
1495
1496/* Add a constant OFFSET to a location expression. */
1497
1498static void
1499loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
1500{
1501 dw_loc_descr_ref loc;
1502 HOST_WIDE_INT *p;
1503
1504 gcc_assert (*list_head != NULL);
1505
1506 if (!offset)
1507 return;
1508
1509 /* Find the end of the chain. */
1510 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
1511 ;
1512
1513 p = NULL;
1514 if (loc->dw_loc_opc == DW_OP_fbreg
1515 || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
1516 p = &loc->dw_loc_oprnd1.v.val_int;
1517 else if (loc->dw_loc_opc == DW_OP_bregx)
1518 p = &loc->dw_loc_oprnd2.v.val_int;
1519
1520 /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
1521 offset. Don't optimize if an signed integer overflow would happen. */
1522 if (p != NULL
1523 && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
1524 || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
1525 *p += offset;
1526
1527 else if (offset > 0)
1528 loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
1529
1530 else
1531 {
1532 loc->dw_loc_next
1533 = uint_loc_descriptor (-(unsigned HOST_WIDE_INT) offset);
1534 add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
1535 }
1536}
1537
1538/* Add a constant OFFSET to a location list. */
1539
1540static void
1541loc_list_plus_const (dw_loc_list_ref list_head, HOST_WIDE_INT offset)
1542{
1543 dw_loc_list_ref d;
1544 for (d = list_head; d != NULL; d = d->dw_loc_next)
1545 loc_descr_plus_const (&d->expr, offset);
1546}
1547
1548#define DWARF_REF_SIZE \
1549 (dwarf_version == 2 ? DWARF2_ADDR_SIZE : DWARF_OFFSET_SIZE)
1550
1551/* The number of bits that can be encoded by largest DW_FORM_dataN.
1552 In DWARF4 and earlier it is DW_FORM_data8 with 64 bits, in DWARF5
1553 DW_FORM_data16 with 128 bits. */
1554#define DWARF_LARGEST_DATA_FORM_BITS \
1555 (dwarf_version >= 5 ? 128 : 64)
1556
1557/* Utility inline function for construction of ops that were GNU extension
1558 before DWARF 5. */
1559static inline enum dwarf_location_atom
1560dwarf_OP (enum dwarf_location_atom op)
1561{
1562 switch (op)
1563 {
1564 case DW_OP_implicit_pointer:
1565 if (dwarf_version < 5)
1566 return DW_OP_GNU_implicit_pointer;
1567 break;
1568
1569 case DW_OP_entry_value:
1570 if (dwarf_version < 5)
1571 return DW_OP_GNU_entry_value;
1572 break;
1573
1574 case DW_OP_const_type:
1575 if (dwarf_version < 5)
1576 return DW_OP_GNU_const_type;
1577 break;
1578
1579 case DW_OP_regval_type:
1580 if (dwarf_version < 5)
1581 return DW_OP_GNU_regval_type;
1582 break;
1583
1584 case DW_OP_deref_type:
1585 if (dwarf_version < 5)
1586 return DW_OP_GNU_deref_type;
1587 break;
1588
1589 case DW_OP_convert:
1590 if (dwarf_version < 5)
1591 return DW_OP_GNU_convert;
1592 break;
1593
1594 case DW_OP_reinterpret:
1595 if (dwarf_version < 5)
1596 return DW_OP_GNU_reinterpret;
1597 break;
1598
1599 default:
1600 break;
1601 }
1602 return op;
1603}
1604
1605/* Similarly for attributes. */
1606static inline enum dwarf_attribute
1607dwarf_AT (enum dwarf_attribute at)
1608{
1609 switch (at)
1610 {
1611 case DW_AT_call_return_pc:
1612 if (dwarf_version < 5)
1613 return DW_AT_low_pc;
1614 break;
1615
1616 case DW_AT_call_tail_call:
1617 if (dwarf_version < 5)
1618 return DW_AT_GNU_tail_call;
1619 break;
1620
1621 case DW_AT_call_origin:
1622 if (dwarf_version < 5)
1623 return DW_AT_abstract_origin;
1624 break;
1625
1626 case DW_AT_call_target:
1627 if (dwarf_version < 5)
1628 return DW_AT_GNU_call_site_target;
1629 break;
1630
1631 case DW_AT_call_target_clobbered:
1632 if (dwarf_version < 5)
1633 return DW_AT_GNU_call_site_target_clobbered;
1634 break;
1635
1636 case DW_AT_call_parameter:
1637 if (dwarf_version < 5)
1638 return DW_AT_abstract_origin;
1639 break;
1640
1641 case DW_AT_call_value:
1642 if (dwarf_version < 5)
1643 return DW_AT_GNU_call_site_value;
1644 break;
1645
1646 case DW_AT_call_data_value:
1647 if (dwarf_version < 5)
1648 return DW_AT_GNU_call_site_data_value;
1649 break;
1650
1651 case DW_AT_call_all_calls:
1652 if (dwarf_version < 5)
1653 return DW_AT_GNU_all_call_sites;
1654 break;
1655
1656 case DW_AT_call_all_tail_calls:
1657 if (dwarf_version < 5)
1658 return DW_AT_GNU_all_tail_call_sites;
1659 break;
1660
1661 case DW_AT_dwo_name:
1662 if (dwarf_version < 5)
1663 return DW_AT_GNU_dwo_name;
1664 break;
1665
1666 default:
1667 break;
1668 }
1669 return at;
1670}
1671
1672/* And similarly for tags. */
1673static inline enum dwarf_tag
1674dwarf_TAG (enum dwarf_tag tag)
1675{
1676 switch (tag)
1677 {
1678 case DW_TAG_call_site:
1679 if (dwarf_version < 5)
1680 return DW_TAG_GNU_call_site;
1681 break;
1682
1683 case DW_TAG_call_site_parameter:
1684 if (dwarf_version < 5)
1685 return DW_TAG_GNU_call_site_parameter;
1686 break;
1687
1688 default:
1689 break;
1690 }
1691 return tag;
1692}
1693
1694static unsigned long int get_base_type_offset (dw_die_ref);
1695
1696/* Return the size of a location descriptor. */
1697
1698static unsigned long
1699size_of_loc_descr (dw_loc_descr_ref loc)
1700{
1701 unsigned long size = 1;
1702
1703 switch (loc->dw_loc_opc)
1704 {
1705 case DW_OP_addr:
1706 size += DWARF2_ADDR_SIZE;
1707 break;
1708 case DW_OP_GNU_addr_index:
1709 case DW_OP_GNU_const_index:
1710 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
1711 size += size_of_uleb128 (loc->dw_loc_oprnd1.val_entry->index);
1712 break;
1713 case DW_OP_const1u:
1714 case DW_OP_const1s:
1715 size += 1;
1716 break;
1717 case DW_OP_const2u:
1718 case DW_OP_const2s:
1719 size += 2;
1720 break;
1721 case DW_OP_const4u:
1722 case DW_OP_const4s:
1723 size += 4;
1724 break;
1725 case DW_OP_const8u:
1726 case DW_OP_const8s:
1727 size += 8;
1728 break;
1729 case DW_OP_constu:
1730 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1731 break;
1732 case DW_OP_consts:
1733 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1734 break;
1735 case DW_OP_pick:
1736 size += 1;
1737 break;
1738 case DW_OP_plus_uconst:
1739 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1740 break;
1741 case DW_OP_skip:
1742 case DW_OP_bra:
1743 size += 2;
1744 break;
1745 case DW_OP_breg0:
1746 case DW_OP_breg1:
1747 case DW_OP_breg2:
1748 case DW_OP_breg3:
1749 case DW_OP_breg4:
1750 case DW_OP_breg5:
1751 case DW_OP_breg6:
1752 case DW_OP_breg7:
1753 case DW_OP_breg8:
1754 case DW_OP_breg9:
1755 case DW_OP_breg10:
1756 case DW_OP_breg11:
1757 case DW_OP_breg12:
1758 case DW_OP_breg13:
1759 case DW_OP_breg14:
1760 case DW_OP_breg15:
1761 case DW_OP_breg16:
1762 case DW_OP_breg17:
1763 case DW_OP_breg18:
1764 case DW_OP_breg19:
1765 case DW_OP_breg20:
1766 case DW_OP_breg21:
1767 case DW_OP_breg22:
1768 case DW_OP_breg23:
1769 case DW_OP_breg24:
1770 case DW_OP_breg25:
1771 case DW_OP_breg26:
1772 case DW_OP_breg27:
1773 case DW_OP_breg28:
1774 case DW_OP_breg29:
1775 case DW_OP_breg30:
1776 case DW_OP_breg31:
1777 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1778 break;
1779 case DW_OP_regx:
1780 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1781 break;
1782 case DW_OP_fbreg:
1783 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1784 break;
1785 case DW_OP_bregx:
1786 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1787 size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1788 break;
1789 case DW_OP_piece:
1790 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1791 break;
1792 case DW_OP_bit_piece:
1793 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1794 size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
1795 break;
1796 case DW_OP_deref_size:
1797 case DW_OP_xderef_size:
1798 size += 1;
1799 break;
1800 case DW_OP_call2:
1801 size += 2;
1802 break;
1803 case DW_OP_call4:
1804 size += 4;
1805 break;
1806 case DW_OP_call_ref:
1807 case DW_OP_GNU_variable_value:
1808 size += DWARF_REF_SIZE;
1809 break;
1810 case DW_OP_implicit_value:
1811 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1812 + loc->dw_loc_oprnd1.v.val_unsigned;
1813 break;
1814 case DW_OP_implicit_pointer:
1815 case DW_OP_GNU_implicit_pointer:
1816 size += DWARF_REF_SIZE + size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1817 break;
1818 case DW_OP_entry_value:
1819 case DW_OP_GNU_entry_value:
1820 {
1821 unsigned long op_size = size_of_locs (loc->dw_loc_oprnd1.v.val_loc);
1822 size += size_of_uleb128 (op_size) + op_size;
1823 break;
1824 }
1825 case DW_OP_const_type:
1826 case DW_OP_GNU_const_type:
1827 {
1828 unsigned long o
1829 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1830 size += size_of_uleb128 (o) + 1;
1831 switch (loc->dw_loc_oprnd2.val_class)
1832 {
1833 case dw_val_class_vec:
1834 size += loc->dw_loc_oprnd2.v.val_vec.length
1835 * loc->dw_loc_oprnd2.v.val_vec.elt_size;
1836 break;
1837 case dw_val_class_const:
1838 size += HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT;
1839 break;
1840 case dw_val_class_const_double:
1841 size += HOST_BITS_PER_DOUBLE_INT / BITS_PER_UNIT;
1842 break;
1843 case dw_val_class_wide_int:
1844 size += (get_full_len (*loc->dw_loc_oprnd2.v.val_wide)
1845 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
1846 break;
1847 default:
1848 gcc_unreachable ();
1849 }
1850 break;
1851 }
1852 case DW_OP_regval_type:
1853 case DW_OP_GNU_regval_type:
1854 {
1855 unsigned long o
1856 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1857 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1858 + size_of_uleb128 (o);
1859 }
1860 break;
1861 case DW_OP_deref_type:
1862 case DW_OP_GNU_deref_type:
1863 {
1864 unsigned long o
1865 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1866 size += 1 + size_of_uleb128 (o);
1867 }
1868 break;
1869 case DW_OP_convert:
1870 case DW_OP_reinterpret:
1871 case DW_OP_GNU_convert:
1872 case DW_OP_GNU_reinterpret:
1873 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
1874 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1875 else
1876 {
1877 unsigned long o
1878 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1879 size += size_of_uleb128 (o);
1880 }
1881 break;
1882 case DW_OP_GNU_parameter_ref:
1883 size += 4;
1884 break;
1885 default:
1886 break;
1887 }
1888
1889 return size;
1890}
1891
1892/* Return the size of a series of location descriptors. */
1893
1894unsigned long
1895size_of_locs (dw_loc_descr_ref loc)
1896{
1897 dw_loc_descr_ref l;
1898 unsigned long size;
1899
1900 /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
1901 field, to avoid writing to a PCH file. */
1902 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1903 {
1904 if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
1905 break;
1906 size += size_of_loc_descr (l);
1907 }
1908 if (! l)
1909 return size;
1910
1911 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1912 {
1913 l->dw_loc_addr = size;
1914 size += size_of_loc_descr (l);
1915 }
1916
1917 return size;
1918}
1919
1920/* Return the size of the value in a DW_AT_discr_value attribute. */
1921
1922static int
1923size_of_discr_value (dw_discr_value *discr_value)
1924{
1925 if (discr_value->pos)
1926 return size_of_uleb128 (discr_value->v.uval);
1927 else
1928 return size_of_sleb128 (discr_value->v.sval);
1929}
1930
1931/* Return the size of the value in a DW_AT_discr_list attribute. */
1932
1933static int
1934size_of_discr_list (dw_discr_list_ref discr_list)
1935{
1936 int size = 0;
1937
1938 for (dw_discr_list_ref list = discr_list;
1939 list != NULL;
1940 list = list->dw_discr_next)
1941 {
1942 /* One byte for the discriminant value descriptor, and then one or two
1943 LEB128 numbers, depending on whether it's a single case label or a
1944 range label. */
1945 size += 1;
1946 size += size_of_discr_value (&list->dw_discr_lower_bound);
1947 if (list->dw_discr_range != 0)
1948 size += size_of_discr_value (&list->dw_discr_upper_bound);
1949 }
1950 return size;
1951}
1952
1953static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
1954static void get_ref_die_offset_label (char *, dw_die_ref);
1955static unsigned long int get_ref_die_offset (dw_die_ref);
1956
1957/* Output location description stack opcode's operands (if any).
1958 The for_eh_or_skip parameter controls whether register numbers are
1959 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
1960 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
1961 info). This should be suppressed for the cases that have not been converted
1962 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
1963
1964static void
1965output_loc_operands (dw_loc_descr_ref loc, int for_eh_or_skip)
1966{
1967 dw_val_ref val1 = &loc->dw_loc_oprnd1;
1968 dw_val_ref val2 = &loc->dw_loc_oprnd2;
1969
1970 switch (loc->dw_loc_opc)
1971 {
1972#ifdef DWARF2_DEBUGGING_INFO
1973 case DW_OP_const2u:
1974 case DW_OP_const2s:
1975 dw2_asm_output_data (2, val1->v.val_int, NULL);
1976 break;
1977 case DW_OP_const4u:
1978 if (loc->dtprel)
1979 {
1980 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1981 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
1982 val1->v.val_addr);
1983 fputc ('\n', asm_out_file);
1984 break;
1985 }
1986 /* FALLTHRU */
1987 case DW_OP_const4s:
1988 dw2_asm_output_data (4, val1->v.val_int, NULL);
1989 break;
1990 case DW_OP_const8u:
1991 if (loc->dtprel)
1992 {
1993 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1994 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
1995 val1->v.val_addr);
1996 fputc ('\n', asm_out_file);
1997 break;
1998 }
1999 /* FALLTHRU */
2000 case DW_OP_const8s:
2001 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2002 dw2_asm_output_data (8, val1->v.val_int, NULL);
2003 break;
2004 case DW_OP_skip:
2005 case DW_OP_bra:
2006 {
2007 int offset;
2008
2009 gcc_assert (val1->val_class == dw_val_class_loc);
2010 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2011
2012 dw2_asm_output_data (2, offset, NULL);
2013 }
2014 break;
2015 case DW_OP_implicit_value:
2016 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2017 switch (val2->val_class)
2018 {
2019 case dw_val_class_const:
2020 dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
2021 break;
2022 case dw_val_class_vec:
2023 {
2024 unsigned int elt_size = val2->v.val_vec.elt_size;
2025 unsigned int len = val2->v.val_vec.length;
2026 unsigned int i;
2027 unsigned char *p;
2028
2029 if (elt_size > sizeof (HOST_WIDE_INT))
2030 {
2031 elt_size /= 2;
2032 len *= 2;
2033 }
2034 for (i = 0, p = (unsigned char *) val2->v.val_vec.array;
2035 i < len;
2036 i++, p += elt_size)
2037 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2038 "fp or vector constant word %u", i);
2039 }
2040 break;
2041 case dw_val_class_const_double:
2042 {
2043 unsigned HOST_WIDE_INT first, second;
2044
2045 if (WORDS_BIG_ENDIAN)
2046 {
2047 first = val2->v.val_double.high;
2048 second = val2->v.val_double.low;
2049 }
2050 else
2051 {
2052 first = val2->v.val_double.low;
2053 second = val2->v.val_double.high;
2054 }
2055 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2056 first, NULL);
2057 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2058 second, NULL);
2059 }
2060 break;
2061 case dw_val_class_wide_int:
2062 {
2063 int i;
2064 int len = get_full_len (*val2->v.val_wide);
2065 if (WORDS_BIG_ENDIAN)
2066 for (i = len - 1; i >= 0; --i)
2067 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2068 val2->v.val_wide->elt (i), NULL);
2069 else
2070 for (i = 0; i < len; ++i)
2071 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2072 val2->v.val_wide->elt (i), NULL);
2073 }
2074 break;
2075 case dw_val_class_addr:
2076 gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
2077 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
2078 break;
2079 default:
2080 gcc_unreachable ();
2081 }
2082 break;
2083#else
2084 case DW_OP_const2u:
2085 case DW_OP_const2s:
2086 case DW_OP_const4u:
2087 case DW_OP_const4s:
2088 case DW_OP_const8u:
2089 case DW_OP_const8s:
2090 case DW_OP_skip:
2091 case DW_OP_bra:
2092 case DW_OP_implicit_value:
2093 /* We currently don't make any attempt to make sure these are
2094 aligned properly like we do for the main unwind info, so
2095 don't support emitting things larger than a byte if we're
2096 only doing unwinding. */
2097 gcc_unreachable ();
2098#endif
2099 case DW_OP_const1u:
2100 case DW_OP_const1s:
2101 dw2_asm_output_data (1, val1->v.val_int, NULL);
2102 break;
2103 case DW_OP_constu:
2104 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2105 break;
2106 case DW_OP_consts:
2107 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2108 break;
2109 case DW_OP_pick:
2110 dw2_asm_output_data (1, val1->v.val_int, NULL);
2111 break;
2112 case DW_OP_plus_uconst:
2113 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2114 break;
2115 case DW_OP_breg0:
2116 case DW_OP_breg1:
2117 case DW_OP_breg2:
2118 case DW_OP_breg3:
2119 case DW_OP_breg4:
2120 case DW_OP_breg5:
2121 case DW_OP_breg6:
2122 case DW_OP_breg7:
2123 case DW_OP_breg8:
2124 case DW_OP_breg9:
2125 case DW_OP_breg10:
2126 case DW_OP_breg11:
2127 case DW_OP_breg12:
2128 case DW_OP_breg13:
2129 case DW_OP_breg14:
2130 case DW_OP_breg15:
2131 case DW_OP_breg16:
2132 case DW_OP_breg17:
2133 case DW_OP_breg18:
2134 case DW_OP_breg19:
2135 case DW_OP_breg20:
2136 case DW_OP_breg21:
2137 case DW_OP_breg22:
2138 case DW_OP_breg23:
2139 case DW_OP_breg24:
2140 case DW_OP_breg25:
2141 case DW_OP_breg26:
2142 case DW_OP_breg27:
2143 case DW_OP_breg28:
2144 case DW_OP_breg29:
2145 case DW_OP_breg30:
2146 case DW_OP_breg31:
2147 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2148 break;
2149 case DW_OP_regx:
2150 {
2151 unsigned r = val1->v.val_unsigned;
2152 if (for_eh_or_skip >= 0)
2153 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2154 gcc_assert (size_of_uleb128 (r)
2155 == size_of_uleb128 (val1->v.val_unsigned));
2156 dw2_asm_output_data_uleb128 (r, NULL);
2157 }
2158 break;
2159 case DW_OP_fbreg:
2160 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2161 break;
2162 case DW_OP_bregx:
2163 {
2164 unsigned r = val1->v.val_unsigned;
2165 if (for_eh_or_skip >= 0)
2166 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2167 gcc_assert (size_of_uleb128 (r)
2168 == size_of_uleb128 (val1->v.val_unsigned));
2169 dw2_asm_output_data_uleb128 (r, NULL);
2170 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2171 }
2172 break;
2173 case DW_OP_piece:
2174 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2175 break;
2176 case DW_OP_bit_piece:
2177 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2178 dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
2179 break;
2180 case DW_OP_deref_size:
2181 case DW_OP_xderef_size:
2182 dw2_asm_output_data (1, val1->v.val_int, NULL);
2183 break;
2184
2185 case DW_OP_addr:
2186 if (loc->dtprel)
2187 {
2188 if (targetm.asm_out.output_dwarf_dtprel)
2189 {
2190 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
2191 DWARF2_ADDR_SIZE,
2192 val1->v.val_addr);
2193 fputc ('\n', asm_out_file);
2194 }
2195 else
2196 gcc_unreachable ();
2197 }
2198 else
2199 {
2200#ifdef DWARF2_DEBUGGING_INFO
2201 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
2202#else
2203 gcc_unreachable ();
2204#endif
2205 }
2206 break;
2207
2208 case DW_OP_GNU_addr_index:
2209 case DW_OP_GNU_const_index:
2210 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
2211 dw2_asm_output_data_uleb128 (loc->dw_loc_oprnd1.val_entry->index,
2212 "(index into .debug_addr)");
2213 break;
2214
2215 case DW_OP_call2:
2216 case DW_OP_call4:
2217 {
2218 unsigned long die_offset
2219 = get_ref_die_offset (val1->v.val_die_ref.die);
2220 /* Make sure the offset has been computed and that we can encode it as
2221 an operand. */
2222 gcc_assert (die_offset > 0
2223 && die_offset <= (loc->dw_loc_opc == DW_OP_call2
2224 ? 0xffff
2225 : 0xffffffff));
2226 dw2_asm_output_data ((loc->dw_loc_opc == DW_OP_call2) ? 2 : 4,
2227 die_offset, NULL);
2228 }
2229 break;
2230
2231 case DW_OP_call_ref:
2232 case DW_OP_GNU_variable_value:
2233 {
2234 char label[MAX_ARTIFICIAL_LABEL_BYTES
2235 + HOST_BITS_PER_WIDE_INT / 2 + 2];
2236 gcc_assert (val1->val_class == dw_val_class_die_ref);
2237 get_ref_die_offset_label (label, val1->v.val_die_ref.die);
2238 dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
2239 }
2240 break;
2241
2242 case DW_OP_implicit_pointer:
2243 case DW_OP_GNU_implicit_pointer:
2244 {
2245 char label[MAX_ARTIFICIAL_LABEL_BYTES
2246 + HOST_BITS_PER_WIDE_INT / 2 + 2];
2247 gcc_assert (val1->val_class == dw_val_class_die_ref);
2248 get_ref_die_offset_label (label, val1->v.val_die_ref.die);
2249 dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
2250 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2251 }
2252 break;
2253
2254 case DW_OP_entry_value:
2255 case DW_OP_GNU_entry_value:
2256 dw2_asm_output_data_uleb128 (size_of_locs (val1->v.val_loc), NULL);
2257 output_loc_sequence (val1->v.val_loc, for_eh_or_skip);
2258 break;
2259
2260 case DW_OP_const_type:
2261 case DW_OP_GNU_const_type:
2262 {
2263 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die), l;
2264 gcc_assert (o);
2265 dw2_asm_output_data_uleb128 (o, NULL);
2266 switch (val2->val_class)
2267 {
2268 case dw_val_class_const:
2269 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2270 dw2_asm_output_data (1, l, NULL);
2271 dw2_asm_output_data (l, val2->v.val_int, NULL);
2272 break;
2273 case dw_val_class_vec:
2274 {
2275 unsigned int elt_size = val2->v.val_vec.elt_size;
2276 unsigned int len = val2->v.val_vec.length;
2277 unsigned int i;
2278 unsigned char *p;
2279
2280 l = len * elt_size;
2281 dw2_asm_output_data (1, l, NULL);
2282 if (elt_size > sizeof (HOST_WIDE_INT))
2283 {
2284 elt_size /= 2;
2285 len *= 2;
2286 }
2287 for (i = 0, p = (unsigned char *) val2->v.val_vec.array;
2288 i < len;
2289 i++, p += elt_size)
2290 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2291 "fp or vector constant word %u", i);
2292 }
2293 break;
2294 case dw_val_class_const_double:
2295 {
2296 unsigned HOST_WIDE_INT first, second;
2297 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2298
2299 dw2_asm_output_data (1, 2 * l, NULL);
2300 if (WORDS_BIG_ENDIAN)
2301 {
2302 first = val2->v.val_double.high;
2303 second = val2->v.val_double.low;
2304 }
2305 else
2306 {
2307 first = val2->v.val_double.low;
2308 second = val2->v.val_double.high;
2309 }
2310 dw2_asm_output_data (l, first, NULL);
2311 dw2_asm_output_data (l, second, NULL);
2312 }
2313 break;
2314 case dw_val_class_wide_int:
2315 {
2316 int i;
2317 int len = get_full_len (*val2->v.val_wide);
2318 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2319
2320 dw2_asm_output_data (1, len * l, NULL);
2321 if (WORDS_BIG_ENDIAN)
2322 for (i = len - 1; i >= 0; --i)
2323 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2324 else
2325 for (i = 0; i < len; ++i)
2326 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2327 }
2328 break;
2329 default:
2330 gcc_unreachable ();
2331 }
2332 }
2333 break;
2334 case DW_OP_regval_type:
2335 case DW_OP_GNU_regval_type:
2336 {
2337 unsigned r = val1->v.val_unsigned;
2338 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2339 gcc_assert (o);
2340 if (for_eh_or_skip >= 0)
2341 {
2342 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2343 gcc_assert (size_of_uleb128 (r)
2344 == size_of_uleb128 (val1->v.val_unsigned));
2345 }
2346 dw2_asm_output_data_uleb128 (r, NULL);
2347 dw2_asm_output_data_uleb128 (o, NULL);
2348 }
2349 break;
2350 case DW_OP_deref_type:
2351 case DW_OP_GNU_deref_type:
2352 {
2353 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2354 gcc_assert (o);
2355 dw2_asm_output_data (1, val1->v.val_int, NULL);
2356 dw2_asm_output_data_uleb128 (o, NULL);
2357 }
2358 break;
2359 case DW_OP_convert:
2360 case DW_OP_reinterpret:
2361 case DW_OP_GNU_convert:
2362 case DW_OP_GNU_reinterpret:
2363 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
2364 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2365 else
2366 {
2367 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die);
2368 gcc_assert (o);
2369 dw2_asm_output_data_uleb128 (o, NULL);
2370 }
2371 break;
2372
2373 case DW_OP_GNU_parameter_ref:
2374 {
2375 unsigned long o;
2376 gcc_assert (val1->val_class == dw_val_class_die_ref);
2377 o = get_ref_die_offset (val1->v.val_die_ref.die);
2378 dw2_asm_output_data (4, o, NULL);
2379 }
2380 break;
2381
2382 default:
2383 /* Other codes have no operands. */
2384 break;
2385 }
2386}
2387
2388/* Output a sequence of location operations.
2389 The for_eh_or_skip parameter controls whether register numbers are
2390 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
2391 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
2392 info). This should be suppressed for the cases that have not been converted
2393 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
2394
2395void
2396output_loc_sequence (dw_loc_descr_ref loc, int for_eh_or_skip)
2397{
2398 for (; loc != NULL; loc = loc->dw_loc_next)
2399 {
2400 enum dwarf_location_atom opc = loc->dw_loc_opc;
2401 /* Output the opcode. */
2402 if (for_eh_or_skip >= 0
2403 && opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2404 {
2405 unsigned r = (opc - DW_OP_breg0);
2406 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2407 gcc_assert (r <= 31);
2408 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2409 }
2410 else if (for_eh_or_skip >= 0
2411 && opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2412 {
2413 unsigned r = (opc - DW_OP_reg0);
2414 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2415 gcc_assert (r <= 31);
2416 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2417 }
2418
2419 dw2_asm_output_data (1, opc,
2420 "%s", dwarf_stack_op_name (opc));
2421
2422 /* Output the operand(s) (if any). */
2423 output_loc_operands (loc, for_eh_or_skip);
2424 }
2425}
2426
2427/* Output location description stack opcode's operands (if any).
2428 The output is single bytes on a line, suitable for .cfi_escape. */
2429
2430static void
2431output_loc_operands_raw (dw_loc_descr_ref loc)
2432{
2433 dw_val_ref val1 = &loc->dw_loc_oprnd1;
2434 dw_val_ref val2 = &loc->dw_loc_oprnd2;
2435
2436 switch (loc->dw_loc_opc)
2437 {
2438 case DW_OP_addr:
2439 case DW_OP_GNU_addr_index:
2440 case DW_OP_GNU_const_index:
2441 case DW_OP_implicit_value:
2442 /* We cannot output addresses in .cfi_escape, only bytes. */
2443 gcc_unreachable ();
2444
2445 case DW_OP_const1u:
2446 case DW_OP_const1s:
2447 case DW_OP_pick:
2448 case DW_OP_deref_size:
2449 case DW_OP_xderef_size:
2450 fputc (',', asm_out_file);
2451 dw2_asm_output_data_raw (1, val1->v.val_int);
2452 break;
2453
2454 case DW_OP_const2u:
2455 case DW_OP_const2s:
2456 fputc (',', asm_out_file);
2457 dw2_asm_output_data_raw (2, val1->v.val_int);
2458 break;
2459
2460 case DW_OP_const4u:
2461 case DW_OP_const4s:
2462 fputc (',', asm_out_file);
2463 dw2_asm_output_data_raw (4, val1->v.val_int);
2464 break;
2465
2466 case DW_OP_const8u:
2467 case DW_OP_const8s:
2468 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2469 fputc (',', asm_out_file);
2470 dw2_asm_output_data_raw (8, val1->v.val_int);
2471 break;
2472
2473 case DW_OP_skip:
2474 case DW_OP_bra:
2475 {
2476 int offset;
2477
2478 gcc_assert (val1->val_class == dw_val_class_loc);
2479 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2480
2481 fputc (',', asm_out_file);
2482 dw2_asm_output_data_raw (2, offset);
2483 }
2484 break;
2485
2486 case DW_OP_regx:
2487 {
2488 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2489 gcc_assert (size_of_uleb128 (r)
2490 == size_of_uleb128 (val1->v.val_unsigned));
2491 fputc (',', asm_out_file);
2492 dw2_asm_output_data_uleb128_raw (r);
2493 }
2494 break;
2495
2496 case DW_OP_constu:
2497 case DW_OP_plus_uconst:
2498 case DW_OP_piece:
2499 fputc (',', asm_out_file);
2500 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2501 break;
2502
2503 case DW_OP_bit_piece:
2504 fputc (',', asm_out_file);
2505 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2506 dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
2507 break;
2508
2509 case DW_OP_consts:
2510 case DW_OP_breg0:
2511 case DW_OP_breg1:
2512 case DW_OP_breg2:
2513 case DW_OP_breg3:
2514 case DW_OP_breg4:
2515 case DW_OP_breg5:
2516 case DW_OP_breg6:
2517 case DW_OP_breg7:
2518 case DW_OP_breg8:
2519 case DW_OP_breg9:
2520 case DW_OP_breg10:
2521 case DW_OP_breg11:
2522 case DW_OP_breg12:
2523 case DW_OP_breg13:
2524 case DW_OP_breg14:
2525 case DW_OP_breg15:
2526 case DW_OP_breg16:
2527 case DW_OP_breg17:
2528 case DW_OP_breg18:
2529 case DW_OP_breg19:
2530 case DW_OP_breg20:
2531 case DW_OP_breg21:
2532 case DW_OP_breg22:
2533 case DW_OP_breg23:
2534 case DW_OP_breg24:
2535 case DW_OP_breg25:
2536 case DW_OP_breg26:
2537 case DW_OP_breg27:
2538 case DW_OP_breg28:
2539 case DW_OP_breg29:
2540 case DW_OP_breg30:
2541 case DW_OP_breg31:
2542 case DW_OP_fbreg:
2543 fputc (',', asm_out_file);
2544 dw2_asm_output_data_sleb128_raw (val1->v.val_int);
2545 break;
2546
2547 case DW_OP_bregx:
2548 {
2549 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2550 gcc_assert (size_of_uleb128 (r)
2551 == size_of_uleb128 (val1->v.val_unsigned));
2552 fputc (',', asm_out_file);
2553 dw2_asm_output_data_uleb128_raw (r);
2554 fputc (',', asm_out_file);
2555 dw2_asm_output_data_sleb128_raw (val2->v.val_int);
2556 }
2557 break;
2558
2559 case DW_OP_implicit_pointer:
2560 case DW_OP_entry_value:
2561 case DW_OP_const_type:
2562 case DW_OP_regval_type:
2563 case DW_OP_deref_type:
2564 case DW_OP_convert:
2565 case DW_OP_reinterpret:
2566 case DW_OP_GNU_implicit_pointer:
2567 case DW_OP_GNU_entry_value:
2568 case DW_OP_GNU_const_type:
2569 case DW_OP_GNU_regval_type:
2570 case DW_OP_GNU_deref_type:
2571 case DW_OP_GNU_convert:
2572 case DW_OP_GNU_reinterpret:
2573 case DW_OP_GNU_parameter_ref:
2574 gcc_unreachable ();
2575 break;
2576
2577 default:
2578 /* Other codes have no operands. */
2579 break;
2580 }
2581}
2582
2583void
2584output_loc_sequence_raw (dw_loc_descr_ref loc)
2585{
2586 while (1)
2587 {
2588 enum dwarf_location_atom opc = loc->dw_loc_opc;
2589 /* Output the opcode. */
2590 if (opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2591 {
2592 unsigned r = (opc - DW_OP_breg0);
2593 r = DWARF2_FRAME_REG_OUT (r, 1);
2594 gcc_assert (r <= 31);
2595 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2596 }
2597 else if (opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2598 {
2599 unsigned r = (opc - DW_OP_reg0);
2600 r = DWARF2_FRAME_REG_OUT (r, 1);
2601 gcc_assert (r <= 31);
2602 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2603 }
2604 /* Output the opcode. */
2605 fprintf (asm_out_file, "%#x", opc);
2606 output_loc_operands_raw (loc);
2607
2608 if (!loc->dw_loc_next)
2609 break;
2610 loc = loc->dw_loc_next;
2611
2612 fputc (',', asm_out_file);
2613 }
2614}
2615
2616/* This function builds a dwarf location descriptor sequence from a
2617 dw_cfa_location, adding the given OFFSET to the result of the
2618 expression. */
2619
2620struct dw_loc_descr_node *
2621build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
2622{
2623 struct dw_loc_descr_node *head, *tmp;
2624
2625 offset += cfa->offset;
2626
2627 if (cfa->indirect)
2628 {
2629 head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
2630 head->dw_loc_oprnd1.val_class = dw_val_class_const;
2631 head->dw_loc_oprnd1.val_entry = NULL;
2632 tmp = new_loc_descr (DW_OP_deref, 0, 0);
2633 add_loc_descr (&head, tmp);
2634 if (offset != 0)
2635 {
2636 tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
2637 add_loc_descr (&head, tmp);
2638 }
2639 }
2640 else
2641 head = new_reg_loc_descr (cfa->reg, offset);
2642
2643 return head;
2644}
2645
2646/* This function builds a dwarf location descriptor sequence for
2647 the address at OFFSET from the CFA when stack is aligned to
2648 ALIGNMENT byte. */
2649
2650struct dw_loc_descr_node *
2651build_cfa_aligned_loc (dw_cfa_location *cfa,
2652 HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
2653{
2654 struct dw_loc_descr_node *head;
2655 unsigned int dwarf_fp
2656 = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
2657
2658 /* When CFA is defined as FP+OFFSET, emulate stack alignment. */
2659 if (cfa->reg == HARD_FRAME_POINTER_REGNUM && cfa->indirect == 0)
2660 {
2661 head = new_reg_loc_descr (dwarf_fp, 0);
2662 add_loc_descr (&head, int_loc_descriptor (alignment));
2663 add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
2664 loc_descr_plus_const (&head, offset);
2665 }
2666 else
2667 head = new_reg_loc_descr (dwarf_fp, offset);
2668 return head;
2669}
2670
2671/* And now, the support for symbolic debugging information. */
2672
2673/* .debug_str support. */
2674
2675static void dwarf2out_init (const char *);
2676static void dwarf2out_finish (const char *);
2677static void dwarf2out_early_finish (const char *);
2678static void dwarf2out_assembly_start (void);
2679static void dwarf2out_define (unsigned int, const char *);
2680static void dwarf2out_undef (unsigned int, const char *);
2681static void dwarf2out_start_source_file (unsigned, const char *);
2682static void dwarf2out_end_source_file (unsigned);
2683static void dwarf2out_function_decl (tree);
2684static void dwarf2out_begin_block (unsigned, unsigned);
2685static void dwarf2out_end_block (unsigned, unsigned);
2686static bool dwarf2out_ignore_block (const_tree);
2687static void dwarf2out_early_global_decl (tree);
2688static void dwarf2out_late_global_decl (tree);
2689static void dwarf2out_type_decl (tree, int);
2690static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
2691static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
2692 dw_die_ref);
2693static void dwarf2out_abstract_function (tree);
2694static void dwarf2out_var_location (rtx_insn *);
2695static void dwarf2out_size_function (tree);
2696static void dwarf2out_begin_function (tree);
2697static void dwarf2out_end_function (unsigned int);
2698static void dwarf2out_register_main_translation_unit (tree unit);
2699static void dwarf2out_set_name (tree, tree);
2700
2701/* The debug hooks structure. */
2702
2703const struct gcc_debug_hooks dwarf2_debug_hooks =
2704{
2705 dwarf2out_init,
2706 dwarf2out_finish,
2707 dwarf2out_early_finish,
2708 dwarf2out_assembly_start,
2709 dwarf2out_define,
2710 dwarf2out_undef,
2711 dwarf2out_start_source_file,
2712 dwarf2out_end_source_file,
2713 dwarf2out_begin_block,
2714 dwarf2out_end_block,
2715 dwarf2out_ignore_block,
2716 dwarf2out_source_line,
2717 dwarf2out_begin_prologue,
2718#if VMS_DEBUGGING_INFO
2719 dwarf2out_vms_end_prologue,
2720 dwarf2out_vms_begin_epilogue,
2721#else
2722 debug_nothing_int_charstar,
2723 debug_nothing_int_charstar,
2724#endif
2725 dwarf2out_end_epilogue,
2726 dwarf2out_begin_function,
2727 dwarf2out_end_function, /* end_function */
2728 dwarf2out_register_main_translation_unit,
2729 dwarf2out_function_decl, /* function_decl */
2730 dwarf2out_early_global_decl,
2731 dwarf2out_late_global_decl,
2732 dwarf2out_type_decl, /* type_decl */
2733 dwarf2out_imported_module_or_decl,
2734 debug_nothing_tree, /* deferred_inline_function */
2735 /* The DWARF 2 backend tries to reduce debugging bloat by not
2736 emitting the abstract description of inline functions until
2737 something tries to reference them. */
2738 dwarf2out_abstract_function, /* outlining_inline_function */
2739 debug_nothing_rtx_code_label, /* label */
2740 debug_nothing_int, /* handle_pch */
2741 dwarf2out_var_location,
2742 dwarf2out_size_function, /* size_function */
2743 dwarf2out_switch_text_section,
2744 dwarf2out_set_name,
2745 1, /* start_end_main_source_file */
2746 TYPE_SYMTAB_IS_DIE /* tree_type_symtab_field */
2747};
2748
2749const struct gcc_debug_hooks dwarf2_lineno_debug_hooks =
2750{
2751 dwarf2out_init,
2752 debug_nothing_charstar,
2753 debug_nothing_charstar,
2754 dwarf2out_assembly_start,
2755 debug_nothing_int_charstar,
2756 debug_nothing_int_charstar,
2757 debug_nothing_int_charstar,
2758 debug_nothing_int,
2759 debug_nothing_int_int, /* begin_block */
2760 debug_nothing_int_int, /* end_block */
2761 debug_true_const_tree, /* ignore_block */
2762 dwarf2out_source_line, /* source_line */
2763 debug_nothing_int_int_charstar, /* begin_prologue */
2764 debug_nothing_int_charstar, /* end_prologue */
2765 debug_nothing_int_charstar, /* begin_epilogue */
2766 debug_nothing_int_charstar, /* end_epilogue */
2767 debug_nothing_tree, /* begin_function */
2768 debug_nothing_int, /* end_function */
2769 debug_nothing_tree, /* register_main_translation_unit */
2770 debug_nothing_tree, /* function_decl */
2771 debug_nothing_tree, /* early_global_decl */
2772 debug_nothing_tree, /* late_global_decl */
2773 debug_nothing_tree_int, /* type_decl */
2774 debug_nothing_tree_tree_tree_bool, /* imported_module_or_decl */
2775 debug_nothing_tree, /* deferred_inline_function */
2776 debug_nothing_tree, /* outlining_inline_function */
2777 debug_nothing_rtx_code_label, /* label */
2778 debug_nothing_int, /* handle_pch */
2779 debug_nothing_rtx_insn, /* var_location */
2780 debug_nothing_tree, /* size_function */
2781 debug_nothing_void, /* switch_text_section */
2782 debug_nothing_tree_tree, /* set_name */
2783 0, /* start_end_main_source_file */
2784 TYPE_SYMTAB_IS_ADDRESS /* tree_type_symtab_field */
2785};
2786
2787/* NOTE: In the comments in this file, many references are made to
2788 "Debugging Information Entries". This term is abbreviated as `DIE'
2789 throughout the remainder of this file. */
2790
2791/* An internal representation of the DWARF output is built, and then
2792 walked to generate the DWARF debugging info. The walk of the internal
2793 representation is done after the entire program has been compiled.
2794 The types below are used to describe the internal representation. */
2795
2796/* Whether to put type DIEs into their own section .debug_types instead
2797 of making them part of the .debug_info section. Only supported for
2798 Dwarf V4 or higher and the user didn't disable them through
2799 -fno-debug-types-section. It is more efficient to put them in a
2800 separate comdat sections since the linker will then be able to
2801 remove duplicates. But not all tools support .debug_types sections
2802 yet. For Dwarf V5 or higher .debug_types doesn't exist any more,
2803 it is DW_UT_type unit type in .debug_info section. */
2804
2805#define use_debug_types (dwarf_version >= 4 && flag_debug_types_section)
2806
2807/* Various DIE's use offsets relative to the beginning of the
2808 .debug_info section to refer to each other. */
2809
2810typedef long int dw_offset;
2811
2812struct comdat_type_node;
2813
2814/* The entries in the line_info table more-or-less mirror the opcodes
2815 that are used in the real dwarf line table. Arrays of these entries
2816 are collected per section when DWARF2_ASM_LINE_DEBUG_INFO is not
2817 supported. */
2818
2819enum dw_line_info_opcode {
2820 /* Emit DW_LNE_set_address; the operand is the label index. */
2821 LI_set_address,
2822
2823 /* Emit a row to the matrix with the given line. This may be done
2824 via any combination of DW_LNS_copy, DW_LNS_advance_line, and
2825 special opcodes. */
2826 LI_set_line,
2827
2828 /* Emit a DW_LNS_set_file. */
2829 LI_set_file,
2830
2831 /* Emit a DW_LNS_set_column. */
2832 LI_set_column,
2833
2834 /* Emit a DW_LNS_negate_stmt; the operand is ignored. */
2835 LI_negate_stmt,
2836
2837 /* Emit a DW_LNS_set_prologue_end/epilogue_begin; the operand is ignored. */
2838 LI_set_prologue_end,
2839 LI_set_epilogue_begin,
2840
2841 /* Emit a DW_LNE_set_discriminator. */
2842 LI_set_discriminator
2843};
2844
2845typedef struct GTY(()) dw_line_info_struct {
2846 enum dw_line_info_opcode opcode;
2847 unsigned int val;
2848} dw_line_info_entry;
2849
2850
2851struct GTY(()) dw_line_info_table {
2852 /* The label that marks the end of this section. */
2853 const char *end_label;
2854
2855 /* The values for the last row of the matrix, as collected in the table.
2856 These are used to minimize the changes to the next row. */
2857 unsigned int file_num;
2858 unsigned int line_num;
2859 unsigned int column_num;
2860 int discrim_num;
2861 bool is_stmt;
2862 bool in_use;
2863
2864 vec<dw_line_info_entry, va_gc> *entries;
2865};
2866
2867
2868/* Each DIE attribute has a field specifying the attribute kind,
2869 a link to the next attribute in the chain, and an attribute value.
2870 Attributes are typically linked below the DIE they modify. */
2871
2872typedef struct GTY(()) dw_attr_struct {
2873 enum dwarf_attribute dw_attr;
2874 dw_val_node dw_attr_val;
2875}
2876dw_attr_node;
2877
2878
2879/* The Debugging Information Entry (DIE) structure. DIEs form a tree.
2880 The children of each node form a circular list linked by
2881 die_sib. die_child points to the node *before* the "first" child node. */
2882
2883typedef struct GTY((chain_circular ("%h.die_sib"), for_user)) die_struct {
2884 union die_symbol_or_type_node
2885 {
2886 const char * GTY ((tag ("0"))) die_symbol;
2887 comdat_type_node *GTY ((tag ("1"))) die_type_node;
2888 }
2889 GTY ((desc ("%0.comdat_type_p"))) die_id;
2890 vec<dw_attr_node, va_gc> *die_attr;
2891 dw_die_ref die_parent;
2892 dw_die_ref die_child;
2893 dw_die_ref die_sib;
2894 dw_die_ref die_definition; /* ref from a specification to its definition */
2895 dw_offset die_offset;
2896 unsigned long die_abbrev;
2897 int die_mark;
2898 unsigned int decl_id;
2899 enum dwarf_tag die_tag;
2900 /* Die is used and must not be pruned as unused. */
2901 BOOL_BITFIELD die_perennial_p : 1;
2902 BOOL_BITFIELD comdat_type_p : 1; /* DIE has a type signature */
2903 /* Whether this DIE was removed from the DIE tree, for example via
2904 prune_unused_types. We don't consider those present from the
2905 DIE lookup routines. */
2906 BOOL_BITFIELD removed : 1;
2907 /* Lots of spare bits. */
2908}
2909die_node;
2910
2911/* Set to TRUE while dwarf2out_early_global_decl is running. */
2912static bool early_dwarf;
2913static bool early_dwarf_finished;
2914struct set_early_dwarf {
2915 bool saved;
2916 set_early_dwarf () : saved(early_dwarf)
2917 {
2918 gcc_assert (! early_dwarf_finished);
2919 early_dwarf = true;
2920 }
2921 ~set_early_dwarf () { early_dwarf = saved; }
2922};
2923
2924/* Evaluate 'expr' while 'c' is set to each child of DIE in order. */
2925#define FOR_EACH_CHILD(die, c, expr) do { \
2926 c = die->die_child; \
2927 if (c) do { \
2928 c = c->die_sib; \
2929 expr; \
2930 } while (c != die->die_child); \
2931} while (0)
2932
2933/* The pubname structure */
2934
2935typedef struct GTY(()) pubname_struct {
2936 dw_die_ref die;
2937 const char *name;
2938}
2939pubname_entry;
2940
2941
2942struct GTY(()) dw_ranges {
2943 const char *label;
2944 /* If this is positive, it's a block number, otherwise it's a
2945 bitwise-negated index into dw_ranges_by_label. */
2946 int num;
2947 /* Index for the range list for DW_FORM_rnglistx. */
2948 unsigned int idx : 31;
2949 /* True if this range might be possibly in a different section
2950 from previous entry. */
2951 unsigned int maybe_new_sec : 1;
2952};
2953
2954/* A structure to hold a macinfo entry. */
2955
2956typedef struct GTY(()) macinfo_struct {
2957 unsigned char code;
2958 unsigned HOST_WIDE_INT lineno;
2959 const char *info;
2960}
2961macinfo_entry;
2962
2963
2964struct GTY(()) dw_ranges_by_label {
2965 const char *begin;
2966 const char *end;
2967};
2968
2969/* The comdat type node structure. */
2970struct GTY(()) comdat_type_node
2971{
2972 dw_die_ref root_die;
2973 dw_die_ref type_die;
2974 dw_die_ref skeleton_die;
2975 char signature[DWARF_TYPE_SIGNATURE_SIZE];
2976 comdat_type_node *next;
2977};
2978
2979/* A list of DIEs for which we can't determine ancestry (parent_die
2980 field) just yet. Later in dwarf2out_finish we will fill in the
2981 missing bits. */
2982typedef struct GTY(()) limbo_die_struct {
2983 dw_die_ref die;
2984 /* The tree for which this DIE was created. We use this to
2985 determine ancestry later. */
2986 tree created_for;
2987 struct limbo_die_struct *next;
2988}
2989limbo_die_node;
2990
2991typedef struct skeleton_chain_struct
2992{
2993 dw_die_ref old_die;
2994 dw_die_ref new_die;
2995 struct skeleton_chain_struct *parent;
2996}
2997skeleton_chain_node;
2998
2999/* Define a macro which returns nonzero for a TYPE_DECL which was
3000 implicitly generated for a type.
3001
3002 Note that, unlike the C front-end (which generates a NULL named
3003 TYPE_DECL node for each complete tagged type, each array type,
3004 and each function type node created) the C++ front-end generates
3005 a _named_ TYPE_DECL node for each tagged type node created.
3006 These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
3007 generate a DW_TAG_typedef DIE for them. Likewise with the Ada
3008 front-end, but for each type, tagged or not. */
3009
3010#define TYPE_DECL_IS_STUB(decl) \
3011 (DECL_NAME (decl) == NULL_TREE \
3012 || (DECL_ARTIFICIAL (decl) \
3013 && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl))) \
3014 /* This is necessary for stub decls that \
3015 appear in nested inline functions. */ \
3016 || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
3017 && (decl_ultimate_origin (decl) \
3018 == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
3019
3020/* Information concerning the compilation unit's programming
3021 language, and compiler version. */
3022
3023/* Fixed size portion of the DWARF compilation unit header. */
3024#define DWARF_COMPILE_UNIT_HEADER_SIZE \
3025 (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE \
3026 + (dwarf_version >= 5 ? 4 : 3))
3027
3028/* Fixed size portion of the DWARF comdat type unit header. */
3029#define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
3030 (DWARF_COMPILE_UNIT_HEADER_SIZE \
3031 + DWARF_TYPE_SIGNATURE_SIZE + DWARF_OFFSET_SIZE)
3032
3033/* Fixed size portion of the DWARF skeleton compilation unit header. */
3034#define DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE \
3035 (DWARF_COMPILE_UNIT_HEADER_SIZE + (dwarf_version >= 5 ? 8 : 0))
3036
3037/* Fixed size portion of public names info. */
3038#define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
3039
3040/* Fixed size portion of the address range info. */
3041#define DWARF_ARANGES_HEADER_SIZE \
3042 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
3043 DWARF2_ADDR_SIZE * 2) \
3044 - DWARF_INITIAL_LENGTH_SIZE)
3045
3046/* Size of padding portion in the address range info. It must be
3047 aligned to twice the pointer size. */
3048#define DWARF_ARANGES_PAD_SIZE \
3049 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
3050 DWARF2_ADDR_SIZE * 2) \
3051 - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
3052
3053/* Use assembler line directives if available. */
3054#ifndef DWARF2_ASM_LINE_DEBUG_INFO
3055#ifdef HAVE_AS_DWARF2_DEBUG_LINE
3056#define DWARF2_ASM_LINE_DEBUG_INFO 1
3057#else
3058#define DWARF2_ASM_LINE_DEBUG_INFO 0
3059#endif
3060#endif
3061
3062/* Minimum line offset in a special line info. opcode.
3063 This value was chosen to give a reasonable range of values. */
3064#define DWARF_LINE_BASE -10
3065
3066/* First special line opcode - leave room for the standard opcodes. */
3067#define DWARF_LINE_OPCODE_BASE ((int)DW_LNS_set_isa + 1)
3068
3069/* Range of line offsets in a special line info. opcode. */
3070#define DWARF_LINE_RANGE (254-DWARF_LINE_OPCODE_BASE+1)
3071
3072/* Flag that indicates the initial value of the is_stmt_start flag.
3073 In the present implementation, we do not mark any lines as
3074 the beginning of a source statement, because that information
3075 is not made available by the GCC front-end. */
3076#define DWARF_LINE_DEFAULT_IS_STMT_START 1
3077
3078/* Maximum number of operations per instruction bundle. */
3079#ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
3080#define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
3081#endif
3082
3083/* This location is used by calc_die_sizes() to keep track
3084 the offset of each DIE within the .debug_info section. */
3085static unsigned long next_die_offset;
3086
3087/* Record the root of the DIE's built for the current compilation unit. */
3088static GTY(()) dw_die_ref single_comp_unit_die;
3089
3090/* A list of type DIEs that have been separated into comdat sections. */
3091static GTY(()) comdat_type_node *comdat_type_list;
3092
3093/* A list of CU DIEs that have been separated. */
3094static GTY(()) limbo_die_node *cu_die_list;
3095
3096/* A list of DIEs with a NULL parent waiting to be relocated. */
3097static GTY(()) limbo_die_node *limbo_die_list;
3098
3099/* A list of DIEs for which we may have to generate
3100 DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set. */
3101static GTY(()) limbo_die_node *deferred_asm_name;
3102
3103struct dwarf_file_hasher : ggc_ptr_hash<dwarf_file_data>
3104{
3105 typedef const char *compare_type;
3106
3107 static hashval_t hash (dwarf_file_data *);
3108 static bool equal (dwarf_file_data *, const char *);
3109};
3110
3111/* Filenames referenced by this compilation unit. */
3112static GTY(()) hash_table<dwarf_file_hasher> *file_table;
3113
3114struct decl_die_hasher : ggc_ptr_hash<die_node>
3115{
3116 typedef tree compare_type;
3117
3118 static hashval_t hash (die_node *);
3119 static bool equal (die_node *, tree);
3120};
3121/* A hash table of references to DIE's that describe declarations.
3122 The key is a DECL_UID() which is a unique number identifying each decl. */
3123static GTY (()) hash_table<decl_die_hasher> *decl_die_table;
3124
3125struct GTY ((for_user)) variable_value_struct {
3126 unsigned int decl_id;
3127 vec<dw_die_ref, va_gc> *dies;
3128};
3129
3130struct variable_value_hasher : ggc_ptr_hash<variable_value_struct>
3131{
3132 typedef tree compare_type;
3133
3134 static hashval_t hash (variable_value_struct *);
3135 static bool equal (variable_value_struct *, tree);
3136};
3137/* A hash table of DIEs that contain DW_OP_GNU_variable_value with
3138 dw_val_class_decl_ref class, indexed by FUNCTION_DECLs which is
3139 DECL_CONTEXT of the referenced VAR_DECLs. */
3140static GTY (()) hash_table<variable_value_hasher> *variable_value_hash;
3141
3142struct block_die_hasher : ggc_ptr_hash<die_struct>
3143{
3144 static hashval_t hash (die_struct *);
3145 static bool equal (die_struct *, die_struct *);
3146};
3147
3148/* A hash table of references to DIE's that describe COMMON blocks.
3149 The key is DECL_UID() ^ die_parent. */
3150static GTY (()) hash_table<block_die_hasher> *common_block_die_table;
3151
3152typedef struct GTY(()) die_arg_entry_struct {
3153 dw_die_ref die;
3154 tree arg;
3155} die_arg_entry;
3156
3157
3158/* Node of the variable location list. */
3159struct GTY ((chain_next ("%h.next"))) var_loc_node {
3160 /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
3161 EXPR_LIST chain. For small bitsizes, bitsize is encoded
3162 in mode of the EXPR_LIST node and first EXPR_LIST operand
3163 is either NOTE_INSN_VAR_LOCATION for a piece with a known
3164 location or NULL for padding. For larger bitsizes,
3165 mode is 0 and first operand is a CONCAT with bitsize
3166 as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
3167 NULL as second operand. */
3168 rtx GTY (()) loc;
3169 const char * GTY (()) label;
3170 struct var_loc_node * GTY (()) next;
3171};
3172
3173/* Variable location list. */
3174struct GTY ((for_user)) var_loc_list_def {
3175 struct var_loc_node * GTY (()) first;
3176
3177 /* Pointer to the last but one or last element of the
3178 chained list. If the list is empty, both first and
3179 last are NULL, if the list contains just one node
3180 or the last node certainly is not redundant, it points
3181 to the last node, otherwise points to the last but one.
3182 Do not mark it for GC because it is marked through the chain. */
3183 struct var_loc_node * GTY ((skip ("%h"))) last;
3184
3185 /* Pointer to the last element before section switch,
3186 if NULL, either sections weren't switched or first
3187 is after section switch. */
3188 struct var_loc_node * GTY ((skip ("%h"))) last_before_switch;
3189
3190 /* DECL_UID of the variable decl. */
3191 unsigned int decl_id;
3192};
3193typedef struct var_loc_list_def var_loc_list;
3194
3195/* Call argument location list. */
3196struct GTY ((chain_next ("%h.next"))) call_arg_loc_node {
3197 rtx GTY (()) call_arg_loc_note;
3198 const char * GTY (()) label;
3199 tree GTY (()) block;
3200 bool tail_call_p;
3201 rtx GTY (()) symbol_ref;
3202 struct call_arg_loc_node * GTY (()) next;
3203};
3204
3205
3206struct decl_loc_hasher : ggc_ptr_hash<var_loc_list>
3207{
3208 typedef const_tree compare_type;
3209
3210 static hashval_t hash (var_loc_list *);
3211 static bool equal (var_loc_list *, const_tree);
3212};
3213
3214/* Table of decl location linked lists. */
3215static GTY (()) hash_table<decl_loc_hasher> *decl_loc_table;
3216
3217/* Head and tail of call_arg_loc chain. */
3218static GTY (()) struct call_arg_loc_node *call_arg_locations;
3219static struct call_arg_loc_node *call_arg_loc_last;
3220
3221/* Number of call sites in the current function. */
3222static int call_site_count = -1;
3223/* Number of tail call sites in the current function. */
3224static int tail_call_site_count = -1;
3225
3226/* A cached location list. */
3227struct GTY ((for_user)) cached_dw_loc_list_def {
3228 /* The DECL_UID of the decl that this entry describes. */
3229 unsigned int decl_id;
3230
3231 /* The cached location list. */
3232 dw_loc_list_ref loc_list;
3233};
3234typedef struct cached_dw_loc_list_def cached_dw_loc_list;
3235
3236struct dw_loc_list_hasher : ggc_ptr_hash<cached_dw_loc_list>
3237{
3238
3239 typedef const_tree compare_type;
3240
3241 static hashval_t hash (cached_dw_loc_list *);
3242 static bool equal (cached_dw_loc_list *, const_tree);
3243};
3244
3245/* Table of cached location lists. */
3246static GTY (()) hash_table<dw_loc_list_hasher> *cached_dw_loc_list_table;
3247
3248/* A vector of references to DIE's that are uniquely identified by their tag,
3249 presence/absence of children DIE's, and list of attribute/value pairs. */
3250static GTY(()) vec<dw_die_ref, va_gc> *abbrev_die_table;
3251
3252/* A hash map to remember the stack usage for DWARF procedures. The value
3253 stored is the stack size difference between before the DWARF procedure
3254 invokation and after it returned. In other words, for a DWARF procedure
3255 that consumes N stack slots and that pushes M ones, this stores M - N. */
3256static hash_map<dw_die_ref, int> *dwarf_proc_stack_usage_map;
3257
3258/* A global counter for generating labels for line number data. */
3259static unsigned int line_info_label_num;
3260
3261/* The current table to which we should emit line number information
3262 for the current function. This will be set up at the beginning of
3263 assembly for the function. */
3264static GTY(()) dw_line_info_table *cur_line_info_table;
3265
3266/* The two default tables of line number info. */
3267static GTY(()) dw_line_info_table *text_section_line_info;
3268static GTY(()) dw_line_info_table *cold_text_section_line_info;
3269
3270/* The set of all non-default tables of line number info. */
3271static GTY(()) vec<dw_line_info_table *, va_gc> *separate_line_info;
3272
3273/* A flag to tell pubnames/types export if there is an info section to
3274 refer to. */
3275static bool info_section_emitted;
3276
3277/* A pointer to the base of a table that contains a list of publicly
3278 accessible names. */
3279static GTY (()) vec<pubname_entry, va_gc> *pubname_table;
3280
3281/* A pointer to the base of a table that contains a list of publicly
3282 accessible types. */
3283static GTY (()) vec<pubname_entry, va_gc> *pubtype_table;
3284
3285/* A pointer to the base of a table that contains a list of macro
3286 defines/undefines (and file start/end markers). */
3287static GTY (()) vec<macinfo_entry, va_gc> *macinfo_table;
3288
3289/* True if .debug_macinfo or .debug_macros section is going to be
3290 emitted. */
3291#define have_macinfo \
3292 ((!XCOFF_DEBUGGING_INFO || HAVE_XCOFF_DWARF_EXTRAS) \
3293 && debug_info_level >= DINFO_LEVEL_VERBOSE \
3294 && !macinfo_table->is_empty ())
3295
3296/* Vector of dies for which we should generate .debug_ranges info. */
3297static GTY (()) vec<dw_ranges, va_gc> *ranges_table;
3298
3299/* Vector of pairs of labels referenced in ranges_table. */
3300static GTY (()) vec<dw_ranges_by_label, va_gc> *ranges_by_label;
3301
3302/* Whether we have location lists that need outputting */
3303static GTY(()) bool have_location_lists;
3304
3305/* Unique label counter. */
3306static GTY(()) unsigned int loclabel_num;
3307
3308/* Unique label counter for point-of-call tables. */
3309static GTY(()) unsigned int poc_label_num;
3310
3311/* The last file entry emitted by maybe_emit_file(). */
3312static GTY(()) struct dwarf_file_data * last_emitted_file;
3313
3314/* Number of internal labels generated by gen_internal_sym(). */
3315static GTY(()) int label_num;
3316
3317static GTY(()) vec<die_arg_entry, va_gc> *tmpl_value_parm_die_table;
3318
3319/* Instances of generic types for which we need to generate debug
3320 info that describe their generic parameters and arguments. That
3321 generation needs to happen once all types are properly laid out so
3322 we do it at the end of compilation. */
3323static GTY(()) vec<tree, va_gc> *generic_type_instances;
3324
3325/* Offset from the "steady-state frame pointer" to the frame base,
3326 within the current function. */
3327static HOST_WIDE_INT frame_pointer_fb_offset;
3328static bool frame_pointer_fb_offset_valid;
3329
3330static vec<dw_die_ref> base_types;
3331
3332/* Flags to represent a set of attribute classes for attributes that represent
3333 a scalar value (bounds, pointers, ...). */
3334enum dw_scalar_form
3335{
3336 dw_scalar_form_constant = 0x01,
3337 dw_scalar_form_exprloc = 0x02,
3338 dw_scalar_form_reference = 0x04
3339};
3340
3341/* Forward declarations for functions defined in this file. */
3342
3343static int is_pseudo_reg (const_rtx);
3344static tree type_main_variant (tree);
3345static int is_tagged_type (const_tree);
3346static const char *dwarf_tag_name (unsigned);
3347static const char *dwarf_attr_name (unsigned);
3348static const char *dwarf_form_name (unsigned);
3349static tree decl_ultimate_origin (const_tree);
3350static tree decl_class_context (tree);
3351static void add_dwarf_attr (dw_die_ref, dw_attr_node *);
3352static inline enum dw_val_class AT_class (dw_attr_node *);
3353static inline unsigned int AT_index (dw_attr_node *);
3354static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
3355static inline unsigned AT_flag (dw_attr_node *);
3356static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
3357static inline HOST_WIDE_INT AT_int (dw_attr_node *);
3358static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
3359static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_node *);
3360static void add_AT_double (dw_die_ref, enum dwarf_attribute,
3361 HOST_WIDE_INT, unsigned HOST_WIDE_INT);
3362static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
3363 unsigned int, unsigned char *);
3364static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
3365static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
3366static inline const char *AT_string (dw_attr_node *);
3367static enum dwarf_form AT_string_form (dw_attr_node *);
3368static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
3369static void add_AT_specification (dw_die_ref, dw_die_ref);
3370static inline dw_die_ref AT_ref (dw_attr_node *);
3371static inline int AT_ref_external (dw_attr_node *);
3372static inline void set_AT_ref_external (dw_attr_node *, int);
3373static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
3374static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
3375static inline dw_loc_descr_ref AT_loc (dw_attr_node *);
3376static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
3377 dw_loc_list_ref);
3378static inline dw_loc_list_ref AT_loc_list (dw_attr_node *);
3379static addr_table_entry *add_addr_table_entry (void *, enum ate_kind);
3380static void remove_addr_table_entry (addr_table_entry *);
3381static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx, bool);
3382static inline rtx AT_addr (dw_attr_node *);
3383static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
3384static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
3385static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
3386static void add_AT_loclistsptr (dw_die_ref, enum dwarf_attribute,
3387 const char *);
3388static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
3389 unsigned HOST_WIDE_INT);
3390static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
3391 unsigned long, bool);
3392static inline const char *AT_lbl (dw_attr_node *);
3393static dw_attr_node *get_AT (dw_die_ref, enum dwarf_attribute);
3394static const char *get_AT_low_pc (dw_die_ref);
3395static const char *get_AT_hi_pc (dw_die_ref);
3396static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
3397static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
3398static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
3399static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
3400static bool is_cxx (void);
3401static bool is_cxx (const_tree);
3402static bool is_fortran (void);
3403static bool is_ada (void);
3404static bool remove_AT (dw_die_ref, enum dwarf_attribute);
3405static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
3406static void add_child_die (dw_die_ref, dw_die_ref);
3407static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
3408static dw_die_ref lookup_type_die (tree);
3409static dw_die_ref strip_naming_typedef (tree, dw_die_ref);
3410static dw_die_ref lookup_type_die_strip_naming_typedef (tree);
3411static void equate_type_number_to_die (tree, dw_die_ref);
3412static dw_die_ref lookup_decl_die (tree);
3413static var_loc_list *lookup_decl_loc (const_tree);
3414static void equate_decl_number_to_die (tree, dw_die_ref);
3415static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *);
3416static void print_spaces (FILE *);
3417static void print_die (dw_die_ref, FILE *);
3418static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
3419static dw_die_ref pop_compile_unit (dw_die_ref);
3420static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
3421static void attr_checksum (dw_attr_node *, struct md5_ctx *, int *);
3422static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
3423static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
3424static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
3425static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
3426static void attr_checksum_ordered (enum dwarf_tag, dw_attr_node *,
3427 struct md5_ctx *, int *);
3428struct checksum_attributes;
3429static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
3430static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
3431static void checksum_die_context (dw_die_ref, struct md5_ctx *);
3432static void generate_type_signature (dw_die_ref, comdat_type_node *);
3433static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
3434static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
3435static int same_attr_p (dw_attr_node *, dw_attr_node *, int *);
3436static int same_die_p (dw_die_ref, dw_die_ref, int *);
3437static int same_die_p_wrap (dw_die_ref, dw_die_ref);
3438static void compute_section_prefix (dw_die_ref);
3439static int is_type_die (dw_die_ref);
3440static int is_comdat_die (dw_die_ref);
3441static int is_symbol_die (dw_die_ref);
3442static inline bool is_template_instantiation (dw_die_ref);
3443static void assign_symbol_names (dw_die_ref);
3444static void break_out_includes (dw_die_ref);
3445static int is_declaration_die (dw_die_ref);
3446static int should_move_die_to_comdat (dw_die_ref);
3447static dw_die_ref clone_as_declaration (dw_die_ref);
3448static dw_die_ref clone_die (dw_die_ref);
3449static dw_die_ref clone_tree (dw_die_ref);
3450static dw_die_ref copy_declaration_context (dw_die_ref, dw_die_ref);
3451static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
3452static void generate_skeleton_bottom_up (skeleton_chain_node *);
3453static dw_die_ref generate_skeleton (dw_die_ref);
3454static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
3455 dw_die_ref,
3456 dw_die_ref);
3457static void break_out_comdat_types (dw_die_ref);
3458static void copy_decls_for_unworthy_types (dw_die_ref);
3459
3460static void add_sibling_attributes (dw_die_ref);
3461static void output_location_lists (dw_die_ref);
3462static int constant_size (unsigned HOST_WIDE_INT);
3463static unsigned long size_of_die (dw_die_ref);
3464static void calc_die_sizes (dw_die_ref);
3465static void calc_base_type_die_sizes (void);
3466static void mark_dies (dw_die_ref);
3467static void unmark_dies (dw_die_ref);
3468static void unmark_all_dies (dw_die_ref);
3469static unsigned long size_of_pubnames (vec<pubname_entry, va_gc> *);
3470static unsigned long size_of_aranges (void);
3471static enum dwarf_form value_format (dw_attr_node *);
3472static void output_value_format (dw_attr_node *);
3473static void output_abbrev_section (void);
3474static void output_die_abbrevs (unsigned long, dw_die_ref);
3475static void output_die_symbol (dw_die_ref);
3476static void output_die (dw_die_ref);
3477static void output_compilation_unit_header (enum dwarf_unit_type);
3478static void output_comp_unit (dw_die_ref, int, const unsigned char *);
3479static void output_comdat_type_unit (comdat_type_node *);
3480static const char *dwarf2_name (tree, int);
3481static void add_pubname (tree, dw_die_ref);
3482static void add_enumerator_pubname (const char *, dw_die_ref);
3483static void add_pubname_string (const char *, dw_die_ref);
3484static void add_pubtype (tree, dw_die_ref);
3485static void output_pubnames (vec<pubname_entry, va_gc> *);
3486static void output_aranges (void);
3487static unsigned int add_ranges (const_tree, bool = false);
3488static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
3489 bool *, bool);
3490static void output_ranges (void);
3491static dw_line_info_table *new_line_info_table (void);
3492static void output_line_info (bool);
3493static void output_file_names (void);
3494static dw_die_ref base_type_die (tree, bool);
3495static int is_base_type (tree);
3496static dw_die_ref subrange_type_die (tree, tree, tree, tree, dw_die_ref);
3497static int decl_quals (const_tree);
3498static dw_die_ref modified_type_die (tree, int, bool, dw_die_ref);
3499static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
3500static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
3501static int type_is_enum (const_tree);
3502static unsigned int dbx_reg_number (const_rtx);
3503static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
3504static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
3505static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
3506 enum var_init_status);
3507static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
3508 enum var_init_status);
3509static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
3510 enum var_init_status);
3511static int is_based_loc (const_rtx);
3512static bool resolve_one_addr (rtx *);
3513static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
3514 enum var_init_status);
3515static dw_loc_descr_ref loc_descriptor (rtx, machine_mode mode,
3516 enum var_init_status);
3517struct loc_descr_context;
3518static void add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref);
3519static void add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list);
3520static dw_loc_list_ref loc_list_from_tree (tree, int,
3521 struct loc_descr_context *);
3522static dw_loc_descr_ref loc_descriptor_from_tree (tree, int,
3523 struct loc_descr_context *);
3524static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
3525static tree field_type (const_tree);
3526static unsigned int simple_type_align_in_bits (const_tree);
3527static unsigned int simple_decl_align_in_bits (const_tree);
3528static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
3529struct vlr_context;
3530static dw_loc_descr_ref field_byte_offset (const_tree, struct vlr_context *,
3531 HOST_WIDE_INT *);
3532static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
3533 dw_loc_list_ref);
3534static void add_data_member_location_attribute (dw_die_ref, tree,
3535 struct vlr_context *);
3536static bool add_const_value_attribute (dw_die_ref, rtx);
3537static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
3538static void insert_wide_int (const wide_int &, unsigned char *, int);
3539static void insert_float (const_rtx, unsigned char *);
3540static rtx rtl_for_decl_location (tree);
3541static bool add_location_or_const_value_attribute (dw_die_ref, tree, bool);
3542static bool tree_add_const_value_attribute (dw_die_ref, tree);
3543static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
3544static void add_name_attribute (dw_die_ref, const char *);
3545static void add_gnat_descriptive_type_attribute (dw_die_ref, tree, dw_die_ref);
3546static void add_comp_dir_attribute (dw_die_ref);
3547static void add_scalar_info (dw_die_ref, enum dwarf_attribute, tree, int,
3548 struct loc_descr_context *);
3549static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree,
3550 struct loc_descr_context *);
3551static void add_subscript_info (dw_die_ref, tree, bool);
3552static void add_byte_size_attribute (dw_die_ref, tree);
3553static void add_alignment_attribute (dw_die_ref, tree);
3554static inline void add_bit_offset_attribute (dw_die_ref, tree,
3555 struct vlr_context *);
3556static void add_bit_size_attribute (dw_die_ref, tree);
3557static void add_prototyped_attribute (dw_die_ref, tree);
3558static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
3559static void add_pure_or_virtual_attribute (dw_die_ref, tree);
3560static void add_src_coords_attributes (dw_die_ref, tree);
3561static void add_name_and_src_coords_attributes (dw_die_ref, tree, bool = false);
3562static void add_discr_value (dw_die_ref, dw_discr_value *);
3563static void add_discr_list (dw_die_ref, dw_discr_list_ref);
3564static inline dw_discr_list_ref AT_discr_list (dw_attr_node *);
3565static void push_decl_scope (tree);
3566static void pop_decl_scope (void);
3567static dw_die_ref scope_die_for (tree, dw_die_ref);
3568static inline int local_scope_p (dw_die_ref);
3569static inline int class_scope_p (dw_die_ref);
3570static inline int class_or_namespace_scope_p (dw_die_ref);
3571static void add_type_attribute (dw_die_ref, tree, int, bool, dw_die_ref);
3572static void add_calling_convention_attribute (dw_die_ref, tree);
3573static const char *type_tag (const_tree);
3574static tree member_declared_type (const_tree);
3575#if 0
3576static const char *decl_start_label (tree);
3577#endif
3578static void gen_array_type_die (tree, dw_die_ref);
3579static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
3580#if 0
3581static void gen_entry_point_die (tree, dw_die_ref);
3582#endif
3583static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
3584static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
3585static dw_die_ref gen_formal_parameter_pack_die (tree, tree, dw_die_ref, tree*);
3586static void gen_unspecified_parameters_die (tree, dw_die_ref);
3587static void gen_formal_types_die (tree, dw_die_ref);
3588static void gen_subprogram_die (tree, dw_die_ref);
3589static void gen_variable_die (tree, tree, dw_die_ref);
3590static void gen_const_die (tree, dw_die_ref);
3591static void gen_label_die (tree, dw_die_ref);
3592static void gen_lexical_block_die (tree, dw_die_ref);
3593static void gen_inlined_subroutine_die (tree, dw_die_ref);
3594static void gen_field_die (tree, struct vlr_context *, dw_die_ref);
3595static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
3596static dw_die_ref gen_compile_unit_die (const char *);
3597static void gen_inheritance_die (tree, tree, tree, dw_die_ref);
3598static void gen_member_die (tree, dw_die_ref);
3599static void gen_struct_or_union_type_die (tree, dw_die_ref,
3600 enum debug_info_usage);
3601static void gen_subroutine_type_die (tree, dw_die_ref);
3602static void gen_typedef_die (tree, dw_die_ref);
3603static void gen_type_die (tree, dw_die_ref);
3604static void gen_block_die (tree, dw_die_ref);
3605static void decls_for_scope (tree, dw_die_ref);
3606static bool is_naming_typedef_decl (const_tree);
3607static inline dw_die_ref get_context_die (tree);
3608static void gen_namespace_die (tree, dw_die_ref);
3609static dw_die_ref gen_namelist_decl (tree, dw_die_ref, tree);
3610static dw_die_ref gen_decl_die (tree, tree, struct vlr_context *, dw_die_ref);
3611static dw_die_ref force_decl_die (tree);
3612static dw_die_ref force_type_die (tree);
3613static dw_die_ref setup_namespace_context (tree, dw_die_ref);
3614static dw_die_ref declare_in_namespace (tree, dw_die_ref);
3615static struct dwarf_file_data * lookup_filename (const char *);
3616static void retry_incomplete_types (void);
3617static void gen_type_die_for_member (tree, tree, dw_die_ref);
3618static void gen_generic_params_dies (tree);
3619static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
3620static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
3621static void splice_child_die (dw_die_ref, dw_die_ref);
3622static int file_info_cmp (const void *, const void *);
3623static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
3624 const char *, const char *);
3625static void output_loc_list (dw_loc_list_ref);
3626static char *gen_internal_sym (const char *);
3627static bool want_pubnames (void);
3628
3629static void prune_unmark_dies (dw_die_ref);
3630static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
3631static void prune_unused_types_mark (dw_die_ref, int);
3632static void prune_unused_types_walk (dw_die_ref);
3633static void prune_unused_types_walk_attribs (dw_die_ref);
3634static void prune_unused_types_prune (dw_die_ref);
3635static void prune_unused_types (void);
3636static int maybe_emit_file (struct dwarf_file_data *fd);
3637static inline const char *AT_vms_delta1 (dw_attr_node *);
3638static inline const char *AT_vms_delta2 (dw_attr_node *);
3639static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
3640 const char *, const char *);
3641static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
3642static void gen_remaining_tmpl_value_param_die_attribute (void);
3643static bool generic_type_p (tree);
3644static void schedule_generic_params_dies_gen (tree t);
3645static void gen_scheduled_generic_parms_dies (void);
3646static void resolve_variable_values (void);
3647
3648static const char *comp_dir_string (void);
3649
3650static void hash_loc_operands (dw_loc_descr_ref, inchash::hash &);
3651
3652/* enum for tracking thread-local variables whose address is really an offset
3653 relative to the TLS pointer, which will need link-time relocation, but will
3654 not need relocation by the DWARF consumer. */
3655
3656enum dtprel_bool
3657{
3658 dtprel_false = 0,
3659 dtprel_true = 1
3660};
3661
3662/* Return the operator to use for an address of a variable. For dtprel_true, we
3663 use DW_OP_const*. For regular variables, which need both link-time
3664 relocation and consumer-level relocation (e.g., to account for shared objects
3665 loaded at a random address), we use DW_OP_addr*. */
3666
3667static inline enum dwarf_location_atom
3668dw_addr_op (enum dtprel_bool dtprel)
3669{
3670 if (dtprel == dtprel_true)
3671 return (dwarf_split_debug_info ? DW_OP_GNU_const_index
3672 : (DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u));
3673 else
3674 return dwarf_split_debug_info ? DW_OP_GNU_addr_index : DW_OP_addr;
3675}
3676
3677/* Return a pointer to a newly allocated address location description. If
3678 dwarf_split_debug_info is true, then record the address with the appropriate
3679 relocation. */
3680static inline dw_loc_descr_ref
3681new_addr_loc_descr (rtx addr, enum dtprel_bool dtprel)
3682{
3683 dw_loc_descr_ref ref = new_loc_descr (dw_addr_op (dtprel), 0, 0);
3684
3685 ref->dw_loc_oprnd1.val_class = dw_val_class_addr;
3686 ref->dw_loc_oprnd1.v.val_addr = addr;
3687 ref->dtprel = dtprel;
3688 if (dwarf_split_debug_info)
3689 ref->dw_loc_oprnd1.val_entry
3690 = add_addr_table_entry (addr,
3691 dtprel ? ate_kind_rtx_dtprel : ate_kind_rtx);
3692 else
3693 ref->dw_loc_oprnd1.val_entry = NULL;
3694
3695 return ref;
3696}
3697
3698/* Section names used to hold DWARF debugging information. */
3699
3700#ifndef DEBUG_INFO_SECTION
3701#define DEBUG_INFO_SECTION ".debug_info"
3702#endif
3703#ifndef DEBUG_DWO_INFO_SECTION
3704#define DEBUG_DWO_INFO_SECTION ".debug_info.dwo"
3705#endif
3706#ifndef DEBUG_ABBREV_SECTION
3707#define DEBUG_ABBREV_SECTION ".debug_abbrev"
3708#endif
3709#ifndef DEBUG_DWO_ABBREV_SECTION
3710#define DEBUG_DWO_ABBREV_SECTION ".debug_abbrev.dwo"
3711#endif
3712#ifndef DEBUG_ARANGES_SECTION
3713#define DEBUG_ARANGES_SECTION ".debug_aranges"
3714#endif
3715#ifndef DEBUG_ADDR_SECTION
3716#define DEBUG_ADDR_SECTION ".debug_addr"
3717#endif
3718#ifndef DEBUG_MACINFO_SECTION
3719#define DEBUG_MACINFO_SECTION ".debug_macinfo"
3720#endif
3721#ifndef DEBUG_DWO_MACINFO_SECTION
3722#define DEBUG_DWO_MACINFO_SECTION ".debug_macinfo.dwo"
3723#endif
3724#ifndef DEBUG_DWO_MACRO_SECTION
3725#define DEBUG_DWO_MACRO_SECTION ".debug_macro.dwo"
3726#endif
3727#ifndef DEBUG_MACRO_SECTION
3728#define DEBUG_MACRO_SECTION ".debug_macro"
3729#endif
3730#ifndef DEBUG_LINE_SECTION
3731#define DEBUG_LINE_SECTION ".debug_line"
3732#endif
3733#ifndef DEBUG_DWO_LINE_SECTION
3734#define DEBUG_DWO_LINE_SECTION ".debug_line.dwo"
3735#endif
3736#ifndef DEBUG_LOC_SECTION
3737#define DEBUG_LOC_SECTION ".debug_loc"
3738#endif
3739#ifndef DEBUG_DWO_LOC_SECTION
3740#define DEBUG_DWO_LOC_SECTION ".debug_loc.dwo"
3741#endif
3742#ifndef DEBUG_LOCLISTS_SECTION
3743#define DEBUG_LOCLISTS_SECTION ".debug_loclists"
3744#endif
3745#ifndef DEBUG_DWO_LOCLISTS_SECTION
3746#define DEBUG_DWO_LOCLISTS_SECTION ".debug_loclists.dwo"
3747#endif
3748#ifndef DEBUG_PUBNAMES_SECTION
3749#define DEBUG_PUBNAMES_SECTION \
3750 ((debug_generate_pub_sections == 2) \
3751 ? ".debug_gnu_pubnames" : ".debug_pubnames")
3752#endif
3753#ifndef DEBUG_PUBTYPES_SECTION
3754#define DEBUG_PUBTYPES_SECTION \
3755 ((debug_generate_pub_sections == 2) \
3756 ? ".debug_gnu_pubtypes" : ".debug_pubtypes")
3757#endif
3758#ifndef DEBUG_STR_OFFSETS_SECTION
3759#define DEBUG_STR_OFFSETS_SECTION ".debug_str_offsets"
3760#endif
3761#ifndef DEBUG_DWO_STR_OFFSETS_SECTION
3762#define DEBUG_DWO_STR_OFFSETS_SECTION ".debug_str_offsets.dwo"
3763#endif
3764#ifndef DEBUG_STR_DWO_SECTION
3765#define DEBUG_STR_DWO_SECTION ".debug_str.dwo"
3766#endif
3767#ifndef DEBUG_STR_SECTION
3768#define DEBUG_STR_SECTION ".debug_str"
3769#endif
3770#ifndef DEBUG_RANGES_SECTION
3771#define DEBUG_RANGES_SECTION ".debug_ranges"
3772#endif
3773#ifndef DEBUG_RNGLISTS_SECTION
3774#define DEBUG_RNGLISTS_SECTION ".debug_rnglists"
3775#endif
3776#ifndef DEBUG_LINE_STR_SECTION
3777#define DEBUG_LINE_STR_SECTION ".debug_line_str"
3778#endif
3779
3780/* Standard ELF section names for compiled code and data. */
3781#ifndef TEXT_SECTION_NAME
3782#define TEXT_SECTION_NAME ".text"
3783#endif
3784
3785/* Section flags for .debug_str section. */
3786#define DEBUG_STR_SECTION_FLAGS \
3787 (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings \
3788 ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1 \
3789 : SECTION_DEBUG)
3790
3791/* Section flags for .debug_str.dwo section. */
3792#define DEBUG_STR_DWO_SECTION_FLAGS (SECTION_DEBUG | SECTION_EXCLUDE)
3793
3794/* Labels we insert at beginning sections we can reference instead of
3795 the section names themselves. */
3796
3797#ifndef TEXT_SECTION_LABEL
3798#define TEXT_SECTION_LABEL "Ltext"
3799#endif
3800#ifndef COLD_TEXT_SECTION_LABEL
3801#define COLD_TEXT_SECTION_LABEL "Ltext_cold"
3802#endif
3803#ifndef DEBUG_LINE_SECTION_LABEL
3804#define DEBUG_LINE_SECTION_LABEL "Ldebug_line"
3805#endif
3806#ifndef DEBUG_SKELETON_LINE_SECTION_LABEL
3807#define DEBUG_SKELETON_LINE_SECTION_LABEL "Lskeleton_debug_line"
3808#endif
3809#ifndef DEBUG_INFO_SECTION_LABEL
3810#define DEBUG_INFO_SECTION_LABEL "Ldebug_info"
3811#endif
3812#ifndef DEBUG_SKELETON_INFO_SECTION_LABEL
3813#define DEBUG_SKELETON_INFO_SECTION_LABEL "Lskeleton_debug_info"
3814#endif
3815#ifndef DEBUG_ABBREV_SECTION_LABEL
3816#define DEBUG_ABBREV_SECTION_LABEL "Ldebug_abbrev"
3817#endif
3818#ifndef DEBUG_SKELETON_ABBREV_SECTION_LABEL
3819#define DEBUG_SKELETON_ABBREV_SECTION_LABEL "Lskeleton_debug_abbrev"
3820#endif
3821#ifndef DEBUG_ADDR_SECTION_LABEL
3822#define DEBUG_ADDR_SECTION_LABEL "Ldebug_addr"
3823#endif
3824#ifndef DEBUG_LOC_SECTION_LABEL
3825#define DEBUG_LOC_SECTION_LABEL "Ldebug_loc"
3826#endif
3827#ifndef DEBUG_RANGES_SECTION_LABEL
3828#define DEBUG_RANGES_SECTION_LABEL "Ldebug_ranges"
3829#endif
3830#ifndef DEBUG_MACINFO_SECTION_LABEL
3831#define DEBUG_MACINFO_SECTION_LABEL "Ldebug_macinfo"
3832#endif
3833#ifndef DEBUG_MACRO_SECTION_LABEL
3834#define DEBUG_MACRO_SECTION_LABEL "Ldebug_macro"
3835#endif
3836#define SKELETON_COMP_DIE_ABBREV 1
3837#define SKELETON_TYPE_DIE_ABBREV 2
3838
3839/* Definitions of defaults for formats and names of various special
3840 (artificial) labels which may be generated within this file (when the -g
3841 options is used and DWARF2_DEBUGGING_INFO is in effect.
3842 If necessary, these may be overridden from within the tm.h file, but
3843 typically, overriding these defaults is unnecessary. */
3844
3845static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3846static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3847static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3848static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3849static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3850static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3851static char debug_skeleton_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3852static char debug_skeleton_abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3853static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3854static char debug_addr_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3855static char debug_skeleton_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3856static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3857static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3858static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
3859static char ranges_base_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
3860
3861#ifndef TEXT_END_LABEL
3862#define TEXT_END_LABEL "Letext"
3863#endif
3864#ifndef COLD_END_LABEL
3865#define COLD_END_LABEL "Letext_cold"
3866#endif
3867#ifndef BLOCK_BEGIN_LABEL
3868#define BLOCK_BEGIN_LABEL "LBB"
3869#endif
3870#ifndef BLOCK_END_LABEL
3871#define BLOCK_END_LABEL "LBE"
3872#endif
3873#ifndef LINE_CODE_LABEL
3874#define LINE_CODE_LABEL "LM"
3875#endif
3876
3877
3878/* Return the root of the DIE's built for the current compilation unit. */
3879static dw_die_ref
3880comp_unit_die (void)
3881{
3882 if (!single_comp_unit_die)
3883 single_comp_unit_die = gen_compile_unit_die (NULL);
3884 return single_comp_unit_die;
3885}
3886
3887/* We allow a language front-end to designate a function that is to be
3888 called to "demangle" any name before it is put into a DIE. */
3889
3890static const char *(*demangle_name_func) (const char *);
3891
3892void
3893dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
3894{
3895 demangle_name_func = func;
3896}
3897
3898/* Test if rtl node points to a pseudo register. */
3899
3900static inline int
3901is_pseudo_reg (const_rtx rtl)
3902{
3903 return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
3904 || (GET_CODE (rtl) == SUBREG
3905 && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
3906}
3907
3908/* Return a reference to a type, with its const and volatile qualifiers
3909 removed. */
3910
3911static inline tree
3912type_main_variant (tree type)
3913{
3914 type = TYPE_MAIN_VARIANT (type);
3915
3916 /* ??? There really should be only one main variant among any group of
3917 variants of a given type (and all of the MAIN_VARIANT values for all
3918 members of the group should point to that one type) but sometimes the C
3919 front-end messes this up for array types, so we work around that bug
3920 here. */
3921 if (TREE_CODE (type) == ARRAY_TYPE)
3922 while (type != TYPE_MAIN_VARIANT (type))
3923 type = TYPE_MAIN_VARIANT (type);
3924
3925 return type;
3926}
3927
3928/* Return nonzero if the given type node represents a tagged type. */
3929
3930static inline int
3931is_tagged_type (const_tree type)
3932{
3933 enum tree_code code = TREE_CODE (type);
3934
3935 return (code == RECORD_TYPE || code == UNION_TYPE
3936 || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
3937}
3938
3939/* Set label to debug_info_section_label + die_offset of a DIE reference. */
3940
3941static void
3942get_ref_die_offset_label (char *label, dw_die_ref ref)
3943{
3944 sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
3945}
3946
3947/* Return die_offset of a DIE reference to a base type. */
3948
3949static unsigned long int
3950get_base_type_offset (dw_die_ref ref)
3951{
3952 if (ref->die_offset)
3953 return ref->die_offset;
3954 if (comp_unit_die ()->die_abbrev)
3955 {
3956 calc_base_type_die_sizes ();
3957 gcc_assert (ref->die_offset);
3958 }
3959 return ref->die_offset;
3960}
3961
3962/* Return die_offset of a DIE reference other than base type. */
3963
3964static unsigned long int
3965get_ref_die_offset (dw_die_ref ref)
3966{
3967 gcc_assert (ref->die_offset);
3968 return ref->die_offset;
3969}
3970
3971/* Convert a DIE tag into its string name. */
3972
3973static const char *
3974dwarf_tag_name (unsigned int tag)
3975{
3976 const char *name = get_DW_TAG_name (tag);
3977
3978 if (name != NULL)
3979 return name;
3980
3981 return "DW_TAG_<unknown>";
3982}
3983
3984/* Convert a DWARF attribute code into its string name. */
3985
3986static const char *
3987dwarf_attr_name (unsigned int attr)
3988{
3989 const char *name;
3990
3991 switch (attr)
3992 {
3993#if VMS_DEBUGGING_INFO
3994 case DW_AT_HP_prologue:
3995 return "DW_AT_HP_prologue";
3996#else
3997 case DW_AT_MIPS_loop_unroll_factor:
3998 return "DW_AT_MIPS_loop_unroll_factor";
3999#endif
4000
4001#if VMS_DEBUGGING_INFO
4002 case DW_AT_HP_epilogue:
4003 return "DW_AT_HP_epilogue";
4004#else
4005 case DW_AT_MIPS_stride:
4006 return "DW_AT_MIPS_stride";
4007#endif
4008 }
4009
4010 name = get_DW_AT_name (attr);
4011
4012 if (name != NULL)
4013 return name;
4014
4015 return "DW_AT_<unknown>";
4016}
4017
4018/* Convert a DWARF value form code into its string name. */
4019
4020static const char *
4021dwarf_form_name (unsigned int form)
4022{
4023 const char *name = get_DW_FORM_name (form);
4024
4025 if (name != NULL)
4026 return name;
4027
4028 return "DW_FORM_<unknown>";
4029}
4030
4031/* Determine the "ultimate origin" of a decl. The decl may be an inlined
4032 instance of an inlined instance of a decl which is local to an inline
4033 function, so we have to trace all of the way back through the origin chain
4034 to find out what sort of node actually served as the original seed for the
4035 given block. */
4036
4037static tree
4038decl_ultimate_origin (const_tree decl)
4039{
4040 if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
4041 return NULL_TREE;
4042
4043 /* DECL_ABSTRACT_ORIGIN can point to itself; ignore that if
4044 we're trying to output the abstract instance of this function. */
4045 if (DECL_ABSTRACT_P (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
4046 return NULL_TREE;
4047
4048 /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
4049 most distant ancestor, this should never happen. */
4050 gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
4051
4052 return DECL_ABSTRACT_ORIGIN (decl);
4053}
4054
4055/* Get the class to which DECL belongs, if any. In g++, the DECL_CONTEXT
4056 of a virtual function may refer to a base class, so we check the 'this'
4057 parameter. */
4058
4059static tree
4060decl_class_context (tree decl)
4061{
4062 tree context = NULL_TREE;
4063
4064 if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
4065 context = DECL_CONTEXT (decl);
4066 else
4067 context = TYPE_MAIN_VARIANT
4068 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
4069
4070 if (context && !TYPE_P (context))
4071 context = NULL_TREE;
4072
4073 return context;
4074}
4075
4076/* Add an attribute/value pair to a DIE. */
4077
4078static inline void
4079add_dwarf_attr (dw_die_ref die, dw_attr_node *attr)
4080{
4081 /* Maybe this should be an assert? */
4082 if (die == NULL)
4083 return;
4084
4085 vec_safe_reserve (die->die_attr, 1);
4086 vec_safe_push (die->die_attr, *attr);
4087}
4088
4089static inline enum dw_val_class
4090AT_class (dw_attr_node *a)
4091{
4092 return a->dw_attr_val.val_class;
4093}
4094
4095/* Return the index for any attribute that will be referenced with a
4096 DW_FORM_GNU_addr_index or DW_FORM_GNU_str_index. String indices
4097 are stored in dw_attr_val.v.val_str for reference counting
4098 pruning. */
4099
4100static inline unsigned int
4101AT_index (dw_attr_node *a)
4102{
4103 if (AT_class (a) == dw_val_class_str)
4104 return a->dw_attr_val.v.val_str->index;
4105 else if (a->dw_attr_val.val_entry != NULL)
4106 return a->dw_attr_val.val_entry->index;
4107 return NOT_INDEXED;
4108}
4109
4110/* Add a flag value attribute to a DIE. */
4111
4112static inline void
4113add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
4114{
4115 dw_attr_node attr;
4116
4117 attr.dw_attr = attr_kind;
4118 attr.dw_attr_val.val_class = dw_val_class_flag;
4119 attr.dw_attr_val.val_entry = NULL;
4120 attr.dw_attr_val.v.val_flag = flag;
4121 add_dwarf_attr (die, &attr);
4122}
4123
4124static inline unsigned
4125AT_flag (dw_attr_node *a)
4126{
4127 gcc_assert (a && AT_class (a) == dw_val_class_flag);
4128 return a->dw_attr_val.v.val_flag;
4129}
4130
4131/* Add a signed integer attribute value to a DIE. */
4132
4133static inline void
4134add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
4135{
4136 dw_attr_node attr;
4137
4138 attr.dw_attr = attr_kind;
4139 attr.dw_attr_val.val_class = dw_val_class_const;
4140 attr.dw_attr_val.val_entry = NULL;
4141 attr.dw_attr_val.v.val_int = int_val;
4142 add_dwarf_attr (die, &attr);
4143}
4144
4145static inline HOST_WIDE_INT
4146AT_int (dw_attr_node *a)
4147{
4148 gcc_assert (a && (AT_class (a) == dw_val_class_const
4149 || AT_class (a) == dw_val_class_const_implicit));
4150 return a->dw_attr_val.v.val_int;
4151}
4152
4153/* Add an unsigned integer attribute value to a DIE. */
4154
4155static inline void
4156add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
4157 unsigned HOST_WIDE_INT unsigned_val)
4158{
4159 dw_attr_node attr;
4160
4161 attr.dw_attr = attr_kind;
4162 attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
4163 attr.dw_attr_val.val_entry = NULL;
4164 attr.dw_attr_val.v.val_unsigned = unsigned_val;
4165 add_dwarf_attr (die, &attr);
4166}
4167
4168static inline unsigned HOST_WIDE_INT
4169AT_unsigned (dw_attr_node *a)
4170{
4171 gcc_assert (a && (AT_class (a) == dw_val_class_unsigned_const
4172 || AT_class (a) == dw_val_class_unsigned_const_implicit));
4173 return a->dw_attr_val.v.val_unsigned;
4174}
4175
4176/* Add an unsigned wide integer attribute value to a DIE. */
4177
4178static inline void
4179add_AT_wide (dw_die_ref die, enum dwarf_attribute attr_kind,
4180 const wide_int& w)
4181{
4182 dw_attr_node attr;
4183
4184 attr.dw_attr = attr_kind;
4185 attr.dw_attr_val.val_class = dw_val_class_wide_int;
4186 attr.dw_attr_val.val_entry = NULL;
4187 attr.dw_attr_val.v.val_wide = ggc_alloc<wide_int> ();
4188 *attr.dw_attr_val.v.val_wide = w;
4189 add_dwarf_attr (die, &attr);
4190}
4191
4192/* Add an unsigned double integer attribute value to a DIE. */
4193
4194static inline void
4195add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
4196 HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
4197{
4198 dw_attr_node attr;
4199
4200 attr.dw_attr = attr_kind;
4201 attr.dw_attr_val.val_class = dw_val_class_const_double;
4202 attr.dw_attr_val.val_entry = NULL;
4203 attr.dw_attr_val.v.val_double.high = high;
4204 attr.dw_attr_val.v.val_double.low = low;
4205 add_dwarf_attr (die, &attr);
4206}
4207
4208/* Add a floating point attribute value to a DIE and return it. */
4209
4210static inline void
4211add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
4212 unsigned int length, unsigned int elt_size, unsigned char *array)
4213{
4214 dw_attr_node attr;
4215
4216 attr.dw_attr = attr_kind;
4217 attr.dw_attr_val.val_class = dw_val_class_vec;
4218 attr.dw_attr_val.val_entry = NULL;
4219 attr.dw_attr_val.v.val_vec.length = length;
4220 attr.dw_attr_val.v.val_vec.elt_size = elt_size;
4221 attr.dw_attr_val.v.val_vec.array = array;
4222 add_dwarf_attr (die, &attr);
4223}
4224
4225/* Add an 8-byte data attribute value to a DIE. */
4226
4227static inline void
4228add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
4229 unsigned char data8[8])
4230{
4231 dw_attr_node attr;
4232
4233 attr.dw_attr = attr_kind;
4234 attr.dw_attr_val.val_class = dw_val_class_data8;
4235 attr.dw_attr_val.val_entry = NULL;
4236 memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
4237 add_dwarf_attr (die, &attr);
4238}
4239
4240/* Add DW_AT_low_pc and DW_AT_high_pc to a DIE. When using
4241 dwarf_split_debug_info, address attributes in dies destined for the
4242 final executable have force_direct set to avoid using indexed
4243 references. */
4244
4245static inline void
4246add_AT_low_high_pc (dw_die_ref die, const char *lbl_low, const char *lbl_high,
4247 bool force_direct)
4248{
4249 dw_attr_node attr;
4250 char * lbl_id;
4251
4252 lbl_id = xstrdup (lbl_low);
4253 attr.dw_attr = DW_AT_low_pc;
4254 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4255 attr.dw_attr_val.v.val_lbl_id = lbl_id;
4256 if (dwarf_split_debug_info && !force_direct)
4257 attr.dw_attr_val.val_entry
4258 = add_addr_table_entry (lbl_id, ate_kind_label);
4259 else
4260 attr.dw_attr_val.val_entry = NULL;
4261 add_dwarf_attr (die, &attr);
4262
4263 attr.dw_attr = DW_AT_high_pc;
4264 if (dwarf_version < 4)
4265 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4266 else
4267 attr.dw_attr_val.val_class = dw_val_class_high_pc;
4268 lbl_id = xstrdup (lbl_high);
4269 attr.dw_attr_val.v.val_lbl_id = lbl_id;
4270 if (attr.dw_attr_val.val_class == dw_val_class_lbl_id
4271 && dwarf_split_debug_info && !force_direct)
4272 attr.dw_attr_val.val_entry
4273 = add_addr_table_entry (lbl_id, ate_kind_label);
4274 else
4275 attr.dw_attr_val.val_entry = NULL;
4276 add_dwarf_attr (die, &attr);
4277}
4278
4279/* Hash and equality functions for debug_str_hash. */
4280
4281hashval_t
4282indirect_string_hasher::hash (indirect_string_node *x)
4283{
4284 return htab_hash_string (x->str);
4285}
4286
4287bool
4288indirect_string_hasher::equal (indirect_string_node *x1, const char *x2)
4289{
4290 return strcmp (x1->str, x2) == 0;
4291}
4292
4293/* Add STR to the given string hash table. */
4294
4295static struct indirect_string_node *
4296find_AT_string_in_table (const char *str,
4297 hash_table<indirect_string_hasher> *table)
4298{
4299 struct indirect_string_node *node;
4300
4301 indirect_string_node **slot
4302 = table->find_slot_with_hash (str, htab_hash_string (str), INSERT);
4303 if (*slot == NULL)
4304 {
4305 node = ggc_cleared_alloc<indirect_string_node> ();
4306 node->str = ggc_strdup (str);
4307 *slot = node;
4308 }
4309 else
4310 node = *slot;
4311
4312 node->refcount++;
4313 return node;
4314}
4315
4316/* Add STR to the indirect string hash table. */
4317
4318static struct indirect_string_node *
4319find_AT_string (const char *str)
4320{
4321 if (! debug_str_hash)
4322 debug_str_hash = hash_table<indirect_string_hasher>::create_ggc (10);
4323
4324 return find_AT_string_in_table (str, debug_str_hash);
4325}
4326
4327/* Add a string attribute value to a DIE. */
4328
4329static inline void
4330add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
4331{
4332 dw_attr_node attr;
4333 struct indirect_string_node *node;
4334
4335 node = find_AT_string (str);
4336
4337 attr.dw_attr = attr_kind;
4338 attr.dw_attr_val.val_class = dw_val_class_str;
4339 attr.dw_attr_val.val_entry = NULL;
4340 attr.dw_attr_val.v.val_str = node;
4341 add_dwarf_attr (die, &attr);
4342}
4343
4344static inline const char *
4345AT_string (dw_attr_node *a)
4346{
4347 gcc_assert (a && AT_class (a) == dw_val_class_str);
4348 return a->dw_attr_val.v.val_str->str;
4349}
4350
4351/* Call this function directly to bypass AT_string_form's logic to put
4352 the string inline in the die. */
4353
4354static void
4355set_indirect_string (struct indirect_string_node *node)
4356{
4357 char label[MAX_ARTIFICIAL_LABEL_BYTES];
4358 /* Already indirect is a no op. */
4359 if (node->form == DW_FORM_strp
4360 || node->form == DW_FORM_line_strp
4361 || node->form == DW_FORM_GNU_str_index)
4362 {
4363 gcc_assert (node->label);
4364 return;
4365 }
4366 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
4367 ++dw2_string_counter;
4368 node->label = xstrdup (label);
4369
4370 if (!dwarf_split_debug_info)
4371 {
4372 node->form = DW_FORM_strp;
4373 node->index = NOT_INDEXED;
4374 }
4375 else
4376 {
4377 node->form = DW_FORM_GNU_str_index;
4378 node->index = NO_INDEX_ASSIGNED;
4379 }
4380}
4381
4382/* Find out whether a string should be output inline in DIE
4383 or out-of-line in .debug_str section. */
4384
4385static enum dwarf_form
4386find_string_form (struct indirect_string_node *node)
4387{
4388 unsigned int len;
4389
4390 if (node->form)
4391 return node->form;
4392
4393 len = strlen (node->str) + 1;
4394
4395 /* If the string is shorter or equal to the size of the reference, it is
4396 always better to put it inline. */
4397 if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
4398 return node->form = DW_FORM_string;
4399
4400 /* If we cannot expect the linker to merge strings in .debug_str
4401 section, only put it into .debug_str if it is worth even in this
4402 single module. */
4403 if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
4404 || ((debug_str_section->common.flags & SECTION_MERGE) == 0
4405 && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
4406 return node->form = DW_FORM_string;
4407
4408 set_indirect_string (node);
4409
4410 return node->form;
4411}
4412
4413/* Find out whether the string referenced from the attribute should be
4414 output inline in DIE or out-of-line in .debug_str section. */
4415
4416static enum dwarf_form
4417AT_string_form (dw_attr_node *a)
4418{
4419 gcc_assert (a && AT_class (a) == dw_val_class_str);
4420 return find_string_form (a->dw_attr_val.v.val_str);
4421}
4422
4423/* Add a DIE reference attribute value to a DIE. */
4424
4425static inline void
4426add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
4427{
4428 dw_attr_node attr;
4429 gcc_checking_assert (targ_die != NULL);
4430
4431 /* With LTO we can end up trying to reference something we didn't create
4432 a DIE for. Avoid crashing later on a NULL referenced DIE. */
4433 if (targ_die == NULL)
4434 return;
4435
4436 attr.dw_attr = attr_kind;
4437 attr.dw_attr_val.val_class = dw_val_class_die_ref;
4438 attr.dw_attr_val.val_entry = NULL;
4439 attr.dw_attr_val.v.val_die_ref.die = targ_die;
4440 attr.dw_attr_val.v.val_die_ref.external = 0;
4441 add_dwarf_attr (die, &attr);
4442}
4443
4444/* Change DIE reference REF to point to NEW_DIE instead. */
4445
4446static inline void
4447change_AT_die_ref (dw_attr_node *ref, dw_die_ref new_die)
4448{
4449 gcc_assert (ref->dw_attr_val.val_class == dw_val_class_die_ref);
4450 ref->dw_attr_val.v.val_die_ref.die = new_die;
4451 ref->dw_attr_val.v.val_die_ref.external = 0;
4452}
4453
4454/* Add an AT_specification attribute to a DIE, and also make the back
4455 pointer from the specification to the definition. */
4456
4457static inline void
4458add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
4459{
4460 add_AT_die_ref (die, DW_AT_specification, targ_die);
4461 gcc_assert (!targ_die->die_definition);
4462 targ_die->die_definition = die;
4463}
4464
4465static inline dw_die_ref
4466AT_ref (dw_attr_node *a)
4467{
4468 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4469 return a->dw_attr_val.v.val_die_ref.die;
4470}
4471
4472static inline int
4473AT_ref_external (dw_attr_node *a)
4474{
4475 if (a && AT_class (a) == dw_val_class_die_ref)
4476 return a->dw_attr_val.v.val_die_ref.external;
4477
4478 return 0;
4479}
4480
4481static inline void
4482set_AT_ref_external (dw_attr_node *a, int i)
4483{
4484 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4485 a->dw_attr_val.v.val_die_ref.external = i;
4486}
4487
4488/* Add an FDE reference attribute value to a DIE. */
4489
4490static inline void
4491add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
4492{
4493 dw_attr_node attr;
4494
4495 attr.dw_attr = attr_kind;
4496 attr.dw_attr_val.val_class = dw_val_class_fde_ref;
4497 attr.dw_attr_val.val_entry = NULL;
4498 attr.dw_attr_val.v.val_fde_index = targ_fde;
4499 add_dwarf_attr (die, &attr);
4500}
4501
4502/* Add a location description attribute value to a DIE. */
4503
4504static inline void
4505add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
4506{
4507 dw_attr_node attr;
4508
4509 attr.dw_attr = attr_kind;
4510 attr.dw_attr_val.val_class = dw_val_class_loc;
4511 attr.dw_attr_val.val_entry = NULL;
4512 attr.dw_attr_val.v.val_loc = loc;
4513 add_dwarf_attr (die, &attr);
4514}
4515
4516static inline dw_loc_descr_ref
4517AT_loc (dw_attr_node *a)
4518{
4519 gcc_assert (a && AT_class (a) == dw_val_class_loc);
4520 return a->dw_attr_val.v.val_loc;
4521}
4522
4523static inline void
4524add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
4525{
4526 dw_attr_node attr;
4527
4528 if (XCOFF_DEBUGGING_INFO && !HAVE_XCOFF_DWARF_EXTRAS)
4529 return;
4530
4531 attr.dw_attr = attr_kind;
4532 attr.dw_attr_val.val_class = dw_val_class_loc_list;
4533 attr.dw_attr_val.val_entry = NULL;
4534 attr.dw_attr_val.v.val_loc_list = loc_list;
4535 add_dwarf_attr (die, &attr);
4536 have_location_lists = true;
4537}
4538
4539static inline dw_loc_list_ref
4540AT_loc_list (dw_attr_node *a)
4541{
4542 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4543 return a->dw_attr_val.v.val_loc_list;
4544}
4545
4546static inline dw_loc_list_ref *
4547AT_loc_list_ptr (dw_attr_node *a)
4548{
4549 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4550 return &a->dw_attr_val.v.val_loc_list;
4551}
4552
4553struct addr_hasher : ggc_ptr_hash<addr_table_entry>
4554{
4555 static hashval_t hash (addr_table_entry *);
4556 static bool equal (addr_table_entry *, addr_table_entry *);
4557};
4558
4559/* Table of entries into the .debug_addr section. */
4560
4561static GTY (()) hash_table<addr_hasher> *addr_index_table;
4562
4563/* Hash an address_table_entry. */
4564
4565hashval_t
4566addr_hasher::hash (addr_table_entry *a)
4567{
4568 inchash::hash hstate;
4569 switch (a->kind)
4570 {
4571 case ate_kind_rtx:
4572 hstate.add_int (0);
4573 break;
4574 case ate_kind_rtx_dtprel:
4575 hstate.add_int (1);
4576 break;
4577 case ate_kind_label:
4578 return htab_hash_string (a->addr.label);
4579 default:
4580 gcc_unreachable ();
4581 }
4582 inchash::add_rtx (a->addr.rtl, hstate);
4583 return hstate.end ();
4584}
4585
4586/* Determine equality for two address_table_entries. */
4587
4588bool
4589addr_hasher::equal (addr_table_entry *a1, addr_table_entry *a2)
4590{
4591 if (a1->kind != a2->kind)
4592 return 0;
4593 switch (a1->kind)
4594 {
4595 case ate_kind_rtx:
4596 case ate_kind_rtx_dtprel:
4597 return rtx_equal_p (a1->addr.rtl, a2->addr.rtl);
4598 case ate_kind_label:
4599 return strcmp (a1->addr.label, a2->addr.label) == 0;
4600 default:
4601 gcc_unreachable ();
4602 }
4603}
4604
4605/* Initialize an addr_table_entry. */
4606
4607void
4608init_addr_table_entry (addr_table_entry *e, enum ate_kind kind, void *addr)
4609{
4610 e->kind = kind;
4611 switch (kind)
4612 {
4613 case ate_kind_rtx:
4614 case ate_kind_rtx_dtprel:
4615 e->addr.rtl = (rtx) addr;
4616 break;
4617 case ate_kind_label:
4618 e->addr.label = (char *) addr;
4619 break;
4620 }
4621 e->refcount = 0;
4622 e->index = NO_INDEX_ASSIGNED;
4623}
4624
4625/* Add attr to the address table entry to the table. Defer setting an
4626 index until output time. */
4627
4628static addr_table_entry *
4629add_addr_table_entry (void *addr, enum ate_kind kind)
4630{
4631 addr_table_entry *node;
4632 addr_table_entry finder;
4633
4634 gcc_assert (dwarf_split_debug_info);
4635 if (! addr_index_table)
4636 addr_index_table = hash_table<addr_hasher>::create_ggc (10);
4637 init_addr_table_entry (&finder, kind, addr);
4638 addr_table_entry **slot = addr_index_table->find_slot (&finder, INSERT);
4639
4640 if (*slot == HTAB_EMPTY_ENTRY)
4641 {
4642 node = ggc_cleared_alloc<addr_table_entry> ();
4643 init_addr_table_entry (node, kind, addr);
4644 *slot = node;
4645 }
4646 else
4647 node = *slot;
4648
4649 node->refcount++;
4650 return node;
4651}
4652
4653/* Remove an entry from the addr table by decrementing its refcount.
4654 Strictly, decrementing the refcount would be enough, but the
4655 assertion that the entry is actually in the table has found
4656 bugs. */
4657
4658static void
4659remove_addr_table_entry (addr_table_entry *entry)
4660{
4661 gcc_assert (dwarf_split_debug_info && addr_index_table);
4662 /* After an index is assigned, the table is frozen. */
4663 gcc_assert (entry->refcount > 0 && entry->index == NO_INDEX_ASSIGNED);
4664 entry->refcount--;
4665}
4666
4667/* Given a location list, remove all addresses it refers to from the
4668 address_table. */
4669
4670static void
4671remove_loc_list_addr_table_entries (dw_loc_descr_ref descr)
4672{
4673 for (; descr; descr = descr->dw_loc_next)
4674 if (descr->dw_loc_oprnd1.val_entry != NULL)
4675 {
4676 gcc_assert (descr->dw_loc_oprnd1.val_entry->index == NO_INDEX_ASSIGNED);
4677 remove_addr_table_entry (descr->dw_loc_oprnd1.val_entry);
4678 }
4679}
4680
4681/* A helper function for dwarf2out_finish called through
4682 htab_traverse. Assign an addr_table_entry its index. All entries
4683 must be collected into the table when this function is called,
4684 because the indexing code relies on htab_traverse to traverse nodes
4685 in the same order for each run. */
4686
4687int
4688index_addr_table_entry (addr_table_entry **h, unsigned int *index)
4689{
4690 addr_table_entry *node = *h;
4691
4692 /* Don't index unreferenced nodes. */
4693 if (node->refcount == 0)
4694 return 1;
4695
4696 gcc_assert (node->index == NO_INDEX_ASSIGNED);
4697 node->index = *index;
4698 *index += 1;
4699
4700 return 1;
4701}
4702
4703/* Add an address constant attribute value to a DIE. When using
4704 dwarf_split_debug_info, address attributes in dies destined for the
4705 final executable should be direct references--setting the parameter
4706 force_direct ensures this behavior. */
4707
4708static inline void
4709add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr,
4710 bool force_direct)
4711{
4712 dw_attr_node attr;
4713
4714 attr.dw_attr = attr_kind;
4715 attr.dw_attr_val.val_class = dw_val_class_addr;
4716 attr.dw_attr_val.v.val_addr = addr;
4717 if (dwarf_split_debug_info && !force_direct)
4718 attr.dw_attr_val.val_entry = add_addr_table_entry (addr, ate_kind_rtx);
4719 else
4720 attr.dw_attr_val.val_entry = NULL;
4721 add_dwarf_attr (die, &attr);
4722}
4723
4724/* Get the RTX from to an address DIE attribute. */
4725
4726static inline rtx
4727AT_addr (dw_attr_node *a)
4728{
4729 gcc_assert (a && AT_class (a) == dw_val_class_addr);
4730 return a->dw_attr_val.v.val_addr;
4731}
4732
4733/* Add a file attribute value to a DIE. */
4734
4735static inline void
4736add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
4737 struct dwarf_file_data *fd)
4738{
4739 dw_attr_node attr;
4740
4741 attr.dw_attr = attr_kind;
4742 attr.dw_attr_val.val_class = dw_val_class_file;
4743 attr.dw_attr_val.val_entry = NULL;
4744 attr.dw_attr_val.v.val_file = fd;
4745 add_dwarf_attr (die, &attr);
4746}
4747
4748/* Get the dwarf_file_data from a file DIE attribute. */
4749
4750static inline struct dwarf_file_data *
4751AT_file (dw_attr_node *a)
4752{
4753 gcc_assert (a && (AT_class (a) == dw_val_class_file
4754 || AT_class (a) == dw_val_class_file_implicit));
4755 return a->dw_attr_val.v.val_file;
4756}
4757
4758/* Add a vms delta attribute value to a DIE. */
4759
4760static inline void
4761add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
4762 const char *lbl1, const char *lbl2)
4763{
4764 dw_attr_node attr;
4765
4766 attr.dw_attr = attr_kind;
4767 attr.dw_attr_val.val_class = dw_val_class_vms_delta;
4768 attr.dw_attr_val.val_entry = NULL;
4769 attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
4770 attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
4771 add_dwarf_attr (die, &attr);
4772}
4773
4774/* Add a label identifier attribute value to a DIE. */
4775
4776static inline void
4777add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind,
4778 const char *lbl_id)
4779{
4780 dw_attr_node attr;
4781
4782 attr.dw_attr = attr_kind;
4783 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4784 attr.dw_attr_val.val_entry = NULL;
4785 attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
4786 if (dwarf_split_debug_info)
4787 attr.dw_attr_val.val_entry
4788 = add_addr_table_entry (attr.dw_attr_val.v.val_lbl_id,
4789 ate_kind_label);
4790 add_dwarf_attr (die, &attr);
4791}
4792
4793/* Add a section offset attribute value to a DIE, an offset into the
4794 debug_line section. */
4795
4796static inline void
4797add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4798 const char *label)
4799{
4800 dw_attr_node attr;
4801
4802 attr.dw_attr = attr_kind;
4803 attr.dw_attr_val.val_class = dw_val_class_lineptr;
4804 attr.dw_attr_val.val_entry = NULL;
4805 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4806 add_dwarf_attr (die, &attr);
4807}
4808
4809/* Add a section offset attribute value to a DIE, an offset into the
4810 debug_loclists section. */
4811
4812static inline void
4813add_AT_loclistsptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4814 const char *label)
4815{
4816 dw_attr_node attr;
4817
4818 attr.dw_attr = attr_kind;
4819 attr.dw_attr_val.val_class = dw_val_class_loclistsptr;
4820 attr.dw_attr_val.val_entry = NULL;
4821 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4822 add_dwarf_attr (die, &attr);
4823}
4824
4825/* Add a section offset attribute value to a DIE, an offset into the
4826 debug_macinfo section. */
4827
4828static inline void
4829add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4830 const char *label)
4831{
4832 dw_attr_node attr;
4833
4834 attr.dw_attr = attr_kind;
4835 attr.dw_attr_val.val_class = dw_val_class_macptr;
4836 attr.dw_attr_val.val_entry = NULL;
4837 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4838 add_dwarf_attr (die, &attr);
4839}
4840
4841/* Add an offset attribute value to a DIE. */
4842
4843static inline void
4844add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
4845 unsigned HOST_WIDE_INT offset)
4846{
4847 dw_attr_node attr;
4848
4849 attr.dw_attr = attr_kind;
4850 attr.dw_attr_val.val_class = dw_val_class_offset;
4851 attr.dw_attr_val.val_entry = NULL;
4852 attr.dw_attr_val.v.val_offset = offset;
4853 add_dwarf_attr (die, &attr);
4854}
4855
4856/* Add a range_list attribute value to a DIE. When using
4857 dwarf_split_debug_info, address attributes in dies destined for the
4858 final executable should be direct references--setting the parameter
4859 force_direct ensures this behavior. */
4860
4861#define UNRELOCATED_OFFSET ((addr_table_entry *) 1)
4862#define RELOCATED_OFFSET (NULL)
4863
4864static void
4865add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
4866 long unsigned int offset, bool force_direct)
4867{
4868 dw_attr_node attr;
4869
4870 attr.dw_attr = attr_kind;
4871 attr.dw_attr_val.val_class = dw_val_class_range_list;
4872 /* For the range_list attribute, use val_entry to store whether the
4873 offset should follow split-debug-info or normal semantics. This
4874 value is read in output_range_list_offset. */
4875 if (dwarf_split_debug_info && !force_direct)
4876 attr.dw_attr_val.val_entry = UNRELOCATED_OFFSET;
4877 else
4878 attr.dw_attr_val.val_entry = RELOCATED_OFFSET;
4879 attr.dw_attr_val.v.val_offset = offset;
4880 add_dwarf_attr (die, &attr);
4881}
4882
4883/* Return the start label of a delta attribute. */
4884
4885static inline const char *
4886AT_vms_delta1 (dw_attr_node *a)
4887{
4888 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4889 return a->dw_attr_val.v.val_vms_delta.lbl1;
4890}
4891
4892/* Return the end label of a delta attribute. */
4893
4894static inline const char *
4895AT_vms_delta2 (dw_attr_node *a)
4896{
4897 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4898 return a->dw_attr_val.v.val_vms_delta.lbl2;
4899}
4900
4901static inline const char *
4902AT_lbl (dw_attr_node *a)
4903{
4904 gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
4905 || AT_class (a) == dw_val_class_lineptr
4906 || AT_class (a) == dw_val_class_macptr
4907 || AT_class (a) == dw_val_class_loclistsptr
4908 || AT_class (a) == dw_val_class_high_pc));
4909 return a->dw_attr_val.v.val_lbl_id;
4910}
4911
4912/* Get the attribute of type attr_kind. */
4913
4914static dw_attr_node *
4915get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4916{
4917 dw_attr_node *a;
4918 unsigned ix;
4919 dw_die_ref spec = NULL;
4920
4921 if (! die)
4922 return NULL;
4923
4924 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4925 if (a->dw_attr == attr_kind)
4926 return a;
4927 else if (a->dw_attr == DW_AT_specification
4928 || a->dw_attr == DW_AT_abstract_origin)
4929 spec = AT_ref (a);
4930
4931 if (spec)
4932 return get_AT (spec, attr_kind);
4933
4934 return NULL;
4935}
4936
4937/* Returns the parent of the declaration of DIE. */
4938
4939static dw_die_ref
4940get_die_parent (dw_die_ref die)
4941{
4942 dw_die_ref t;
4943
4944 if (!die)
4945 return NULL;
4946
4947 if ((t = get_AT_ref (die, DW_AT_abstract_origin))
4948 || (t = get_AT_ref (die, DW_AT_specification)))
4949 die = t;
4950
4951 return die->die_parent;
4952}
4953
4954/* Return the "low pc" attribute value, typically associated with a subprogram
4955 DIE. Return null if the "low pc" attribute is either not present, or if it
4956 cannot be represented as an assembler label identifier. */
4957
4958static inline const char *
4959get_AT_low_pc (dw_die_ref die)
4960{
4961 dw_attr_node *a = get_AT (die, DW_AT_low_pc);
4962
4963 return a ? AT_lbl (a) : NULL;
4964}
4965
4966/* Return the "high pc" attribute value, typically associated with a subprogram
4967 DIE. Return null if the "high pc" attribute is either not present, or if it
4968 cannot be represented as an assembler label identifier. */
4969
4970static inline const char *
4971get_AT_hi_pc (dw_die_ref die)
4972{
4973 dw_attr_node *a = get_AT (die, DW_AT_high_pc);
4974
4975 return a ? AT_lbl (a) : NULL;
4976}
4977
4978/* Return the value of the string attribute designated by ATTR_KIND, or
4979 NULL if it is not present. */
4980
4981static inline const char *
4982get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
4983{
4984 dw_attr_node *a = get_AT (die, attr_kind);
4985
4986 return a ? AT_string (a) : NULL;
4987}
4988
4989/* Return the value of the flag attribute designated by ATTR_KIND, or -1
4990 if it is not present. */
4991
4992static inline int
4993get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
4994{
4995 dw_attr_node *a = get_AT (die, attr_kind);
4996
4997 return a ? AT_flag (a) : 0;
4998}
4999
5000/* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
5001 if it is not present. */
5002
5003static inline unsigned
5004get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
5005{
5006 dw_attr_node *a = get_AT (die, attr_kind);
5007
5008 return a ? AT_unsigned (a) : 0;
5009}
5010
5011static inline dw_die_ref
5012get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
5013{
5014 dw_attr_node *a = get_AT (die, attr_kind);
5015
5016 return a ? AT_ref (a) : NULL;
5017}
5018
5019static inline struct dwarf_file_data *
5020get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
5021{
5022 dw_attr_node *a = get_AT (die, attr_kind);
5023
5024 return a ? AT_file (a) : NULL;
5025}
5026
5027/* Return TRUE if the language is C++. */
5028
5029static inline bool
5030is_cxx (void)
5031{
5032 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5033
5034 return (lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus
5035 || lang == DW_LANG_C_plus_plus_11 || lang == DW_LANG_C_plus_plus_14);
5036}
5037
5038/* Return TRUE if DECL was created by the C++ frontend. */
5039
5040static bool
5041is_cxx (const_tree decl)
5042{
5043 if (in_lto_p)
5044 {
5045 const_tree context = decl;
5046 while (context && TREE_CODE (context) != TRANSLATION_UNIT_DECL)
5047 {
5048 if (TREE_CODE (context) == BLOCK)
5049 context = BLOCK_SUPERCONTEXT (context);
5050 else
5051 context = get_containing_scope (context);
5052 }
5053 if (context && TRANSLATION_UNIT_LANGUAGE (context))
5054 return strncmp (TRANSLATION_UNIT_LANGUAGE (context), "GNU C++", 7) == 0;
5055 }
5056 return is_cxx ();
5057}
5058
5059/* Return TRUE if the language is Java. */
5060
5061static inline bool
5062is_java (void)
5063{
5064 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5065
5066 return lang == DW_LANG_Java;
5067}
5068
5069/* Return TRUE if the language is Fortran. */
5070
5071static inline bool
5072is_fortran (void)
5073{
5074 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5075
5076 return (lang == DW_LANG_Fortran77
5077 || lang == DW_LANG_Fortran90
5078 || lang == DW_LANG_Fortran95
5079 || lang == DW_LANG_Fortran03
5080 || lang == DW_LANG_Fortran08);
5081}
5082
5083/* Return TRUE if the language is Ada. */
5084
5085static inline bool
5086is_ada (void)
5087{
5088 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5089
5090 return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
5091}
5092
5093/* Remove the specified attribute if present. Return TRUE if removal
5094 was successful. */
5095
5096static bool
5097remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
5098{
5099 dw_attr_node *a;
5100 unsigned ix;
5101
5102 if (! die)
5103 return false;
5104
5105 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5106 if (a->dw_attr == attr_kind)
5107 {
5108 if (AT_class (a) == dw_val_class_str)
5109 if (a->dw_attr_val.v.val_str->refcount)
5110 a->dw_attr_val.v.val_str->refcount--;
5111
5112 /* vec::ordered_remove should help reduce the number of abbrevs
5113 that are needed. */
5114 die->die_attr->ordered_remove (ix);
5115 return true;
5116 }
5117 return false;
5118}
5119
5120/* Remove CHILD from its parent. PREV must have the property that
5121 PREV->DIE_SIB == CHILD. Does not alter CHILD. */
5122
5123static void
5124remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
5125{
5126 gcc_assert (child->die_parent == prev->die_parent);
5127 gcc_assert (prev->die_sib == child);
5128 if (prev == child)
5129 {
5130 gcc_assert (child->die_parent->die_child == child);
5131 prev = NULL;
5132 }
5133 else
5134 prev->die_sib = child->die_sib;
5135 if (child->die_parent->die_child == child)
5136 child->die_parent->die_child = prev;
5137 child->die_sib = NULL;
5138}
5139
5140/* Replace OLD_CHILD with NEW_CHILD. PREV must have the property that
5141 PREV->DIE_SIB == OLD_CHILD. Does not alter OLD_CHILD. */
5142
5143static void
5144replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
5145{
5146 dw_die_ref parent = old_child->die_parent;
5147
5148 gcc_assert (parent == prev->die_parent);
5149 gcc_assert (prev->die_sib == old_child);
5150
5151 new_child->die_parent = parent;
5152 if (prev == old_child)
5153 {
5154 gcc_assert (parent->die_child == old_child);
5155 new_child->die_sib = new_child;
5156 }
5157 else
5158 {
5159 prev->die_sib = new_child;
5160 new_child->die_sib = old_child->die_sib;
5161 }
5162 if (old_child->die_parent->die_child == old_child)
5163 old_child->die_parent->die_child = new_child;
5164 old_child->die_sib = NULL;
5165}
5166
5167/* Move all children from OLD_PARENT to NEW_PARENT. */
5168
5169static void
5170move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
5171{
5172 dw_die_ref c;
5173 new_parent->die_child = old_parent->die_child;
5174 old_parent->die_child = NULL;
5175 FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
5176}
5177
5178/* Remove child DIE whose die_tag is TAG. Do nothing if no child
5179 matches TAG. */
5180
5181static void
5182remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
5183{
5184 dw_die_ref c;
5185
5186 c = die->die_child;
5187 if (c) do {
5188 dw_die_ref prev = c;
5189 c = c->die_sib;
5190 while (c->die_tag == tag)
5191 {
5192 remove_child_with_prev (c, prev);
5193 c->die_parent = NULL;
5194 /* Might have removed every child. */
5195 if (die->die_child == NULL)
5196 return;
5197 c = prev->die_sib;
5198 }
5199 } while (c != die->die_child);
5200}
5201
5202/* Add a CHILD_DIE as the last child of DIE. */
5203
5204static void
5205add_child_die (dw_die_ref die, dw_die_ref child_die)
5206{
5207 /* FIXME this should probably be an assert. */
5208 if (! die || ! child_die)
5209 return;
5210 gcc_assert (die != child_die);
5211
5212 child_die->die_parent = die;
5213 if (die->die_child)
5214 {
5215 child_die->die_sib = die->die_child->die_sib;
5216 die->die_child->die_sib = child_die;
5217 }
5218 else
5219 child_die->die_sib = child_die;
5220 die->die_child = child_die;
5221}
5222
5223/* Like add_child_die, but put CHILD_DIE after AFTER_DIE. */
5224
5225static void
5226add_child_die_after (dw_die_ref die, dw_die_ref child_die,
5227 dw_die_ref after_die)
5228{
5229 gcc_assert (die
5230 && child_die
5231 && after_die
5232 && die->die_child
5233 && die != child_die);
5234
5235 child_die->die_parent = die;
5236 child_die->die_sib = after_die->die_sib;
5237 after_die->die_sib = child_die;
5238 if (die->die_child == after_die)
5239 die->die_child = child_die;
5240}
5241
5242/* Unassociate CHILD from its parent, and make its parent be
5243 NEW_PARENT. */
5244
5245static void
5246reparent_child (dw_die_ref child, dw_die_ref new_parent)
5247{
5248 for (dw_die_ref p = child->die_parent->die_child; ; p = p->die_sib)
5249 if (p->die_sib == child)
5250 {
5251 remove_child_with_prev (child, p);
5252 break;
5253 }
5254 add_child_die (new_parent, child);
5255}
5256
5257/* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
5258 is the specification, to the end of PARENT's list of children.
5259 This is done by removing and re-adding it. */
5260
5261static void
5262splice_child_die (dw_die_ref parent, dw_die_ref child)
5263{
5264 /* We want the declaration DIE from inside the class, not the
5265 specification DIE at toplevel. */
5266 if (child->die_parent != parent)
5267 {
5268 dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
5269
5270 if (tmp)
5271 child = tmp;
5272 }
5273
5274 gcc_assert (child->die_parent == parent
5275 || (child->die_parent
5276 == get_AT_ref (parent, DW_AT_specification)));
5277
5278 reparent_child (child, parent);
5279}
5280
5281/* Create and return a new die with a parent of PARENT_DIE. If
5282 PARENT_DIE is NULL, the new DIE is placed in limbo and an
5283 associated tree T must be supplied to determine parenthood
5284 later. */
5285
5286static inline dw_die_ref
5287new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
5288{
5289 dw_die_ref die = ggc_cleared_alloc<die_node> ();
5290
5291 die->die_tag = tag_value;
5292
5293 if (parent_die != NULL)
5294 add_child_die (parent_die, die);
5295 else
5296 {
5297 limbo_die_node *limbo_node;
5298
5299 /* No DIEs created after early dwarf should end up in limbo,
5300 because the limbo list should not persist past LTO
5301 streaming. */
5302 if (tag_value != DW_TAG_compile_unit
5303 /* These are allowed because they're generated while
5304 breaking out COMDAT units late. */
5305 && tag_value != DW_TAG_type_unit
5306 && tag_value != DW_TAG_skeleton_unit
5307 && !early_dwarf
5308 /* Allow nested functions to live in limbo because they will
5309 only temporarily live there, as decls_for_scope will fix
5310 them up. */
5311 && (TREE_CODE (t) != FUNCTION_DECL
5312 || !decl_function_context (t))
5313 /* Same as nested functions above but for types. Types that
5314 are local to a function will be fixed in
5315 decls_for_scope. */
5316 && (!RECORD_OR_UNION_TYPE_P (t)
5317 || !TYPE_CONTEXT (t)
5318 || TREE_CODE (TYPE_CONTEXT (t)) != FUNCTION_DECL)
5319 /* FIXME debug-early: Allow late limbo DIE creation for LTO,
5320 especially in the ltrans stage, but once we implement LTO
5321 dwarf streaming, we should remove this exception. */
5322 && !in_lto_p)
5323 {
5324 fprintf (stderr, "symbol ended up in limbo too late:");
5325 debug_generic_stmt (t);
5326 gcc_unreachable ();
5327 }
5328
5329 limbo_node = ggc_cleared_alloc<limbo_die_node> ();
5330 limbo_node->die = die;
5331 limbo_node->created_for = t;
5332 limbo_node->next = limbo_die_list;
5333 limbo_die_list = limbo_node;
5334 }
5335
5336 return die;
5337}
5338
5339/* Return the DIE associated with the given type specifier. */
5340
5341static inline dw_die_ref
5342lookup_type_die (tree type)
5343{
5344 dw_die_ref die = TYPE_SYMTAB_DIE (type);
5345 if (die && die->removed)
5346 {
5347 TYPE_SYMTAB_DIE (type) = NULL;
5348 return NULL;
5349 }
5350 return die;
5351}
5352
5353/* Given a TYPE_DIE representing the type TYPE, if TYPE is an
5354 anonymous type named by the typedef TYPE_DIE, return the DIE of the
5355 anonymous type instead the one of the naming typedef. */
5356
5357static inline dw_die_ref
5358strip_naming_typedef (tree type, dw_die_ref type_die)
5359{
5360 if (type
5361 && TREE_CODE (type) == RECORD_TYPE
5362 && type_die
5363 && type_die->die_tag == DW_TAG_typedef
5364 && is_naming_typedef_decl (TYPE_NAME (type)))
5365 type_die = get_AT_ref (type_die, DW_AT_type);
5366 return type_die;
5367}
5368
5369/* Like lookup_type_die, but if type is an anonymous type named by a
5370 typedef[1], return the DIE of the anonymous type instead the one of
5371 the naming typedef. This is because in gen_typedef_die, we did
5372 equate the anonymous struct named by the typedef with the DIE of
5373 the naming typedef. So by default, lookup_type_die on an anonymous
5374 struct yields the DIE of the naming typedef.
5375
5376 [1]: Read the comment of is_naming_typedef_decl to learn about what
5377 a naming typedef is. */
5378
5379static inline dw_die_ref
5380lookup_type_die_strip_naming_typedef (tree type)
5381{
5382 dw_die_ref die = lookup_type_die (type);
5383 return strip_naming_typedef (type, die);
5384}
5385
5386/* Equate a DIE to a given type specifier. */
5387
5388static inline void
5389equate_type_number_to_die (tree type, dw_die_ref type_die)
5390{
5391 TYPE_SYMTAB_DIE (type) = type_die;
5392}
5393
5394/* Returns a hash value for X (which really is a die_struct). */
5395
5396inline hashval_t
5397decl_die_hasher::hash (die_node *x)
5398{
5399 return (hashval_t) x->decl_id;
5400}
5401
5402/* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
5403
5404inline bool
5405decl_die_hasher::equal (die_node *x, tree y)
5406{
5407 return (x->decl_id == DECL_UID (y));
5408}
5409
5410/* Return the DIE associated with a given declaration. */
5411
5412static inline dw_die_ref
5413lookup_decl_die (tree decl)
5414{
5415 dw_die_ref *die = decl_die_table->find_slot_with_hash (decl, DECL_UID (decl),
5416 NO_INSERT);
5417 if (!die)
5418 return NULL;
5419 if ((*die)->removed)
5420 {
5421 decl_die_table->clear_slot (die);
5422 return NULL;
5423 }
5424 return *die;
5425}
5426
5427/* Returns a hash value for X (which really is a var_loc_list). */
5428
5429inline hashval_t
5430decl_loc_hasher::hash (var_loc_list *x)
5431{
5432 return (hashval_t) x->decl_id;
5433}
5434
5435/* Return nonzero if decl_id of var_loc_list X is the same as
5436 UID of decl *Y. */
5437
5438inline bool
5439decl_loc_hasher::equal (var_loc_list *x, const_tree y)
5440{
5441 return (x->decl_id == DECL_UID (y));
5442}
5443
5444/* Return the var_loc list associated with a given declaration. */
5445
5446static inline var_loc_list *
5447lookup_decl_loc (const_tree decl)
5448{
5449 if (!decl_loc_table)
5450 return NULL;
5451 return decl_loc_table->find_with_hash (decl, DECL_UID (decl));
5452}
5453
5454/* Returns a hash value for X (which really is a cached_dw_loc_list_list). */
5455
5456inline hashval_t
5457dw_loc_list_hasher::hash (cached_dw_loc_list *x)
5458{
5459 return (hashval_t) x->decl_id;
5460}
5461
5462/* Return nonzero if decl_id of cached_dw_loc_list X is the same as
5463 UID of decl *Y. */
5464
5465inline bool
5466dw_loc_list_hasher::equal (cached_dw_loc_list *x, const_tree y)
5467{
5468 return (x->decl_id == DECL_UID (y));
5469}
5470
5471/* Equate a DIE to a particular declaration. */
5472
5473static void
5474equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
5475{
5476 unsigned int decl_id = DECL_UID (decl);
5477
5478 *decl_die_table->find_slot_with_hash (decl, decl_id, INSERT) = decl_die;
5479 decl_die->decl_id = decl_id;
5480}
5481
5482/* Return how many bits covers PIECE EXPR_LIST. */
5483
5484static HOST_WIDE_INT
5485decl_piece_bitsize (rtx piece)
5486{
5487 int ret = (int) GET_MODE (piece);
5488 if (ret)
5489 return ret;
5490 gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
5491 && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
5492 return INTVAL (XEXP (XEXP (piece, 0), 0));
5493}
5494
5495/* Return pointer to the location of location note in PIECE EXPR_LIST. */
5496
5497static rtx *
5498decl_piece_varloc_ptr (rtx piece)
5499{
5500 if ((int) GET_MODE (piece))
5501 return &XEXP (piece, 0);
5502 else
5503 return &XEXP (XEXP (piece, 0), 1);
5504}
5505
5506/* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
5507 Next is the chain of following piece nodes. */
5508
5509static rtx_expr_list *
5510decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
5511{
5512 if (bitsize > 0 && bitsize <= (int) MAX_MACHINE_MODE)
5513 return alloc_EXPR_LIST (bitsize, loc_note, next);
5514 else
5515 return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
5516 GEN_INT (bitsize),
5517 loc_note), next);
5518}
5519
5520/* Return rtx that should be stored into loc field for
5521 LOC_NOTE and BITPOS/BITSIZE. */
5522
5523static rtx
5524construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
5525 HOST_WIDE_INT bitsize)
5526{
5527 if (bitsize != -1)
5528 {
5529 loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
5530 if (bitpos != 0)
5531 loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
5532 }
5533 return loc_note;
5534}
5535
5536/* This function either modifies location piece list *DEST in
5537 place (if SRC and INNER is NULL), or copies location piece list
5538 *SRC to *DEST while modifying it. Location BITPOS is modified
5539 to contain LOC_NOTE, any pieces overlapping it are removed resp.
5540 not copied and if needed some padding around it is added.
5541 When modifying in place, DEST should point to EXPR_LIST where
5542 earlier pieces cover PIECE_BITPOS bits, when copying SRC points
5543 to the start of the whole list and INNER points to the EXPR_LIST
5544 where earlier pieces cover PIECE_BITPOS bits. */
5545
5546static void
5547adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
5548 HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
5549 HOST_WIDE_INT bitsize, rtx loc_note)
5550{
5551 HOST_WIDE_INT diff;
5552 bool copy = inner != NULL;
5553
5554 if (copy)
5555 {
5556 /* First copy all nodes preceding the current bitpos. */
5557 while (src != inner)
5558 {
5559 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5560 decl_piece_bitsize (*src), NULL_RTX);
5561 dest = &XEXP (*dest, 1);
5562 src = &XEXP (*src, 1);
5563 }
5564 }
5565 /* Add padding if needed. */
5566 if (bitpos != piece_bitpos)
5567 {
5568 *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
5569 copy ? NULL_RTX : *dest);
5570 dest = &XEXP (*dest, 1);
5571 }
5572 else if (*dest && decl_piece_bitsize (*dest) == bitsize)
5573 {
5574 gcc_assert (!copy);
5575 /* A piece with correct bitpos and bitsize already exist,
5576 just update the location for it and return. */
5577 *decl_piece_varloc_ptr (*dest) = loc_note;
5578 return;
5579 }
5580 /* Add the piece that changed. */
5581 *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
5582 dest = &XEXP (*dest, 1);
5583 /* Skip over pieces that overlap it. */
5584 diff = bitpos - piece_bitpos + bitsize;
5585 if (!copy)
5586 src = dest;
5587 while (diff > 0 && *src)
5588 {
5589 rtx piece = *src;
5590 diff -= decl_piece_bitsize (piece);
5591 if (copy)
5592 src = &XEXP (piece, 1);
5593 else
5594 {
5595 *src = XEXP (piece, 1);
5596 free_EXPR_LIST_node (piece);
5597 }
5598 }
5599 /* Add padding if needed. */
5600 if (diff < 0 && *src)
5601 {
5602 if (!copy)
5603 dest = src;
5604 *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
5605 dest = &XEXP (*dest, 1);
5606 }
5607 if (!copy)
5608 return;
5609 /* Finally copy all nodes following it. */
5610 while (*src)
5611 {
5612 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5613 decl_piece_bitsize (*src), NULL_RTX);
5614 dest = &XEXP (*dest, 1);
5615 src = &XEXP (*src, 1);
5616 }
5617}
5618
5619/* Add a variable location node to the linked list for DECL. */
5620
5621static struct var_loc_node *
5622add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
5623{
5624 unsigned int decl_id;
5625 var_loc_list *temp;
5626 struct var_loc_node *loc = NULL;
5627 HOST_WIDE_INT bitsize = -1, bitpos = -1;
5628
5629 if (VAR_P (decl) && DECL_HAS_DEBUG_EXPR_P (decl))
5630 {
5631 tree realdecl = DECL_DEBUG_EXPR (decl);
5632 if (handled_component_p (realdecl)
5633 || (TREE_CODE (realdecl) == MEM_REF
5634 && TREE_CODE (TREE_OPERAND (realdecl, 0)) == ADDR_EXPR))
5635 {
5636 HOST_WIDE_INT maxsize;
5637 bool reverse;
5638 tree innerdecl
5639 = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize,
5640 &reverse);
5641 if (!DECL_P (innerdecl)
5642 || DECL_IGNORED_P (innerdecl)
5643 || TREE_STATIC (innerdecl)
5644 || bitsize <= 0
5645 || bitpos + bitsize > 256
5646 || bitsize != maxsize)
5647 return NULL;
5648 decl = innerdecl;
5649 }
5650 }
5651
5652 decl_id = DECL_UID (decl);
5653 var_loc_list **slot
5654 = decl_loc_table->find_slot_with_hash (decl, decl_id, INSERT);
5655 if (*slot == NULL)
5656 {
5657 temp = ggc_cleared_alloc<var_loc_list> ();
5658 temp->decl_id = decl_id;
5659 *slot = temp;
5660 }
5661 else
5662 temp = *slot;
5663
5664 /* For PARM_DECLs try to keep around the original incoming value,
5665 even if that means we'll emit a zero-range .debug_loc entry. */
5666 if (temp->last
5667 && temp->first == temp->last
5668 && TREE_CODE (decl) == PARM_DECL
5669 && NOTE_P (temp->first->loc)
5670 && NOTE_VAR_LOCATION_DECL (temp->first->loc) == decl
5671 && DECL_INCOMING_RTL (decl)
5672 && NOTE_VAR_LOCATION_LOC (temp->first->loc)
5673 && GET_CODE (NOTE_VAR_LOCATION_LOC (temp->first->loc))
5674 == GET_CODE (DECL_INCOMING_RTL (decl))
5675 && prev_real_insn (as_a<rtx_insn *> (temp->first->loc)) == NULL_RTX
5676 && (bitsize != -1
5677 || !rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->first->loc),
5678 NOTE_VAR_LOCATION_LOC (loc_note))
5679 || (NOTE_VAR_LOCATION_STATUS (temp->first->loc)
5680 != NOTE_VAR_LOCATION_STATUS (loc_note))))
5681 {
5682 loc = ggc_cleared_alloc<var_loc_node> ();
5683 temp->first->next = loc;
5684 temp->last = loc;
5685 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5686 }
5687 else if (temp->last)
5688 {
5689 struct var_loc_node *last = temp->last, *unused = NULL;
5690 rtx *piece_loc = NULL, last_loc_note;
5691 HOST_WIDE_INT piece_bitpos = 0;
5692 if (last->next)
5693 {
5694 last = last->next;
5695 gcc_assert (last->next == NULL);
5696 }
5697 if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
5698 {
5699 piece_loc = &last->loc;
5700 do
5701 {
5702 HOST_WIDE_INT cur_bitsize = decl_piece_bitsize (*piece_loc);
5703 if (piece_bitpos + cur_bitsize > bitpos)
5704 break;
5705 piece_bitpos += cur_bitsize;
5706 piece_loc = &XEXP (*piece_loc, 1);
5707 }
5708 while (*piece_loc);
5709 }
5710 /* TEMP->LAST here is either pointer to the last but one or
5711 last element in the chained list, LAST is pointer to the
5712 last element. */
5713 if (label && strcmp (last->label, label) == 0)
5714 {
5715 /* For SRA optimized variables if there weren't any real
5716 insns since last note, just modify the last node. */
5717 if (piece_loc != NULL)
5718 {
5719 adjust_piece_list (piece_loc, NULL, NULL,
5720 bitpos, piece_bitpos, bitsize, loc_note);
5721 return NULL;
5722 }
5723 /* If the last note doesn't cover any instructions, remove it. */
5724 if (temp->last != last)
5725 {
5726 temp->last->next = NULL;
5727 unused = last;
5728 last = temp->last;
5729 gcc_assert (strcmp (last->label, label) != 0);
5730 }
5731 else
5732 {
5733 gcc_assert (temp->first == temp->last
5734 || (temp->first->next == temp->last
5735 && TREE_CODE (decl) == PARM_DECL));
5736 memset (temp->last, '\0', sizeof (*temp->last));
5737 temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
5738 return temp->last;
5739 }
5740 }
5741 if (bitsize == -1 && NOTE_P (last->loc))
5742 last_loc_note = last->loc;
5743 else if (piece_loc != NULL
5744 && *piece_loc != NULL_RTX
5745 && piece_bitpos == bitpos
5746 && decl_piece_bitsize (*piece_loc) == bitsize)
5747 last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
5748 else
5749 last_loc_note = NULL_RTX;
5750 /* If the current location is the same as the end of the list,
5751 and either both or neither of the locations is uninitialized,
5752 we have nothing to do. */
5753 if (last_loc_note == NULL_RTX
5754 || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
5755 NOTE_VAR_LOCATION_LOC (loc_note)))
5756 || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5757 != NOTE_VAR_LOCATION_STATUS (loc_note))
5758 && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5759 == VAR_INIT_STATUS_UNINITIALIZED)
5760 || (NOTE_VAR_LOCATION_STATUS (loc_note)
5761 == VAR_INIT_STATUS_UNINITIALIZED))))
5762 {
5763 /* Add LOC to the end of list and update LAST. If the last
5764 element of the list has been removed above, reuse its
5765 memory for the new node, otherwise allocate a new one. */
5766 if (unused)
5767 {
5768 loc = unused;
5769 memset (loc, '\0', sizeof (*loc));
5770 }
5771 else
5772 loc = ggc_cleared_alloc<var_loc_node> ();
5773 if (bitsize == -1 || piece_loc == NULL)
5774 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5775 else
5776 adjust_piece_list (&loc->loc, &last->loc, piece_loc,
5777 bitpos, piece_bitpos, bitsize, loc_note);
5778 last->next = loc;
5779 /* Ensure TEMP->LAST will point either to the new last but one
5780 element of the chain, or to the last element in it. */
5781 if (last != temp->last)
5782 temp->last = last;
5783 }
5784 else if (unused)
5785 ggc_free (unused);
5786 }
5787 else
5788 {
5789 loc = ggc_cleared_alloc<var_loc_node> ();
5790 temp->first = loc;
5791 temp->last = loc;
5792 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5793 }
5794 return loc;
5795}
5796
5797/* Keep track of the number of spaces used to indent the
5798 output of the debugging routines that print the structure of
5799 the DIE internal representation. */
5800static int print_indent;
5801
5802/* Indent the line the number of spaces given by print_indent. */
5803
5804static inline void
5805print_spaces (FILE *outfile)
5806{
5807 fprintf (outfile, "%*s", print_indent, "");
5808}
5809
5810/* Print a type signature in hex. */
5811
5812static inline void
5813print_signature (FILE *outfile, char *sig)
5814{
5815 int i;
5816
5817 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
5818 fprintf (outfile, "%02x", sig[i] & 0xff);
5819}
5820
5821static inline void
5822print_discr_value (FILE *outfile, dw_discr_value *discr_value)
5823{
5824 if (discr_value->pos)
5825 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, discr_value->v.sval);
5826 else
5827 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, discr_value->v.uval);
5828}
5829
5830static void print_loc_descr (dw_loc_descr_ref, FILE *);
5831
5832/* Print the value associated to the VAL DWARF value node to OUTFILE. If
5833 RECURSE, output location descriptor operations. */
5834
5835static void
5836print_dw_val (dw_val_node *val, bool recurse, FILE *outfile)
5837{
5838 switch (val->val_class)
5839 {
5840 case dw_val_class_addr:
5841 fprintf (outfile, "address");
5842 break;
5843 case dw_val_class_offset:
5844 fprintf (outfile, "offset");
5845 break;
5846 case dw_val_class_loc:
5847 fprintf (outfile, "location descriptor");
5848 if (val->v.val_loc == NULL)
5849 fprintf (outfile, " -> <null>\n");
5850 else if (recurse)
5851 {
5852 fprintf (outfile, ":\n");
5853 print_indent += 4;
5854 print_loc_descr (val->v.val_loc, outfile);
5855 print_indent -= 4;
5856 }
5857 else
5858 fprintf (outfile, " (%p)\n", (void *) val->v.val_loc);
5859 break;
5860 case dw_val_class_loc_list:
5861 fprintf (outfile, "location list -> label:%s",
5862 val->v.val_loc_list->ll_symbol);
5863 break;
5864 case dw_val_class_range_list:
5865 fprintf (outfile, "range list");
5866 break;
5867 case dw_val_class_const:
5868 case dw_val_class_const_implicit:
5869 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, val->v.val_int);
5870 break;
5871 case dw_val_class_unsigned_const:
5872 case dw_val_class_unsigned_const_implicit:
5873 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, val->v.val_unsigned);
5874 break;
5875 case dw_val_class_const_double:
5876 fprintf (outfile, "constant (" HOST_WIDE_INT_PRINT_DEC","\
5877 HOST_WIDE_INT_PRINT_UNSIGNED")",
5878 val->v.val_double.high,
5879 val->v.val_double.low);
5880 break;
5881 case dw_val_class_wide_int:
5882 {
5883 int i = val->v.val_wide->get_len ();
5884 fprintf (outfile, "constant (");
5885 gcc_assert (i > 0);
5886 if (val->v.val_wide->elt (i - 1) == 0)
5887 fprintf (outfile, "0x");
5888 fprintf (outfile, HOST_WIDE_INT_PRINT_HEX,
5889 val->v.val_wide->elt (--i));
5890 while (--i >= 0)
5891 fprintf (outfile, HOST_WIDE_INT_PRINT_PADDED_HEX,
5892 val->v.val_wide->elt (i));
5893 fprintf (outfile, ")");
5894 break;
5895 }
5896 case dw_val_class_vec:
5897 fprintf (outfile, "floating-point or vector constant");
5898 break;
5899 case dw_val_class_flag:
5900 fprintf (outfile, "%u", val->v.val_flag);
5901 break;
5902 case dw_val_class_die_ref:
5903 if (val->v.val_die_ref.die != NULL)
5904 {
5905 dw_die_ref die = val->v.val_die_ref.die;
5906
5907 if (die->comdat_type_p)
5908 {
5909 fprintf (outfile, "die -> signature: ");
5910 print_signature (outfile,
5911 die->die_id.die_type_node->signature);
5912 }
5913 else if (die->die_id.die_symbol)
5914 fprintf (outfile, "die -> label: %s", die->die_id.die_symbol);
5915 else
5916 fprintf (outfile, "die -> %ld", die->die_offset);
5917 fprintf (outfile, " (%p)", (void *) die);
5918 }
5919 else
5920 fprintf (outfile, "die -> <null>");
5921 break;
5922 case dw_val_class_vms_delta:
5923 fprintf (outfile, "delta: @slotcount(%s-%s)",
5924 val->v.val_vms_delta.lbl2, val->v.val_vms_delta.lbl1);
5925 break;
5926 case dw_val_class_lbl_id:
5927 case dw_val_class_lineptr:
5928 case dw_val_class_macptr:
5929 case dw_val_class_loclistsptr:
5930 case dw_val_class_high_pc:
5931 fprintf (outfile, "label: %s", val->v.val_lbl_id);
5932 break;
5933 case dw_val_class_str:
5934 if (val->v.val_str->str != NULL)
5935 fprintf (outfile, "\"%s\"", val->v.val_str->str);
5936 else
5937 fprintf (outfile, "<null>");
5938 break;
5939 case dw_val_class_file:
5940 case dw_val_class_file_implicit:
5941 fprintf (outfile, "\"%s\" (%d)", val->v.val_file->filename,
5942 val->v.val_file->emitted_number);
5943 break;
5944 case dw_val_class_data8:
5945 {
5946 int i;
5947
5948 for (i = 0; i < 8; i++)
5949 fprintf (outfile, "%02x", val->v.val_data8[i]);
5950 break;
5951 }
5952 case dw_val_class_discr_value:
5953 print_discr_value (outfile, &val->v.val_discr_value);
5954 break;
5955 case dw_val_class_discr_list:
5956 for (dw_discr_list_ref node = val->v.val_discr_list;
5957 node != NULL;
5958 node = node->dw_discr_next)
5959 {
5960 if (node->dw_discr_range)
5961 {
5962 fprintf (outfile, " .. ");
5963 print_discr_value (outfile, &node->dw_discr_lower_bound);
5964 print_discr_value (outfile, &node->dw_discr_upper_bound);
5965 }
5966 else
5967 print_discr_value (outfile, &node->dw_discr_lower_bound);
5968
5969 if (node->dw_discr_next != NULL)
5970 fprintf (outfile, " | ");
5971 }
5972 default:
5973 break;
5974 }
5975}
5976
5977/* Likewise, for a DIE attribute. */
5978
5979static void
5980print_attribute (dw_attr_node *a, bool recurse, FILE *outfile)
5981{
5982 print_dw_val (&a->dw_attr_val, recurse, outfile);
5983}
5984
5985
5986/* Print the list of operands in the LOC location description to OUTFILE. This
5987 routine is a debugging aid only. */
5988
5989static void
5990print_loc_descr (dw_loc_descr_ref loc, FILE *outfile)
5991{
5992 dw_loc_descr_ref l = loc;
5993
5994 if (loc == NULL)
5995 {
5996 print_spaces (outfile);
5997 fprintf (outfile, "<null>\n");
5998 return;
5999 }
6000
6001 for (l = loc; l != NULL; l = l->dw_loc_next)
6002 {
6003 print_spaces (outfile);
6004 fprintf (outfile, "(%p) %s",
6005 (void *) l,
6006 dwarf_stack_op_name (l->dw_loc_opc));
6007 if (l->dw_loc_oprnd1.val_class != dw_val_class_none)
6008 {
6009 fprintf (outfile, " ");
6010 print_dw_val (&l->dw_loc_oprnd1, false, outfile);
6011 }
6012 if (l->dw_loc_oprnd2.val_class != dw_val_class_none)
6013 {
6014 fprintf (outfile, ", ");
6015 print_dw_val (&l->dw_loc_oprnd2, false, outfile);
6016 }
6017 fprintf (outfile, "\n");
6018 }
6019}
6020
6021/* Print the information associated with a given DIE, and its children.
6022 This routine is a debugging aid only. */
6023
6024static void
6025print_die (dw_die_ref die, FILE *outfile)
6026{
6027 dw_attr_node *a;
6028 dw_die_ref c;
6029 unsigned ix;
6030
6031 print_spaces (outfile);
6032 fprintf (outfile, "DIE %4ld: %s (%p)\n",
6033 die->die_offset, dwarf_tag_name (die->die_tag),
6034 (void*) die);
6035 print_spaces (outfile);
6036 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
6037 fprintf (outfile, " offset: %ld", die->die_offset);
6038 fprintf (outfile, " mark: %d\n", die->die_mark);
6039
6040 if (die->comdat_type_p)
6041 {
6042 print_spaces (outfile);
6043 fprintf (outfile, " signature: ");
6044 print_signature (outfile, die->die_id.die_type_node->signature);
6045 fprintf (outfile, "\n");
6046 }
6047
6048 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6049 {
6050 print_spaces (outfile);
6051 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
6052
6053 print_attribute (a, true, outfile);
6054 fprintf (outfile, "\n");
6055 }
6056
6057 if (die->die_child != NULL)
6058 {
6059 print_indent += 4;
6060 FOR_EACH_CHILD (die, c, print_die (c, outfile));
6061 print_indent -= 4;
6062 }
6063 if (print_indent == 0)
6064 fprintf (outfile, "\n");
6065}
6066
6067/* Print the list of operations in the LOC location description. */
6068
6069DEBUG_FUNCTION void
6070debug_dwarf_loc_descr (dw_loc_descr_ref loc)
6071{
6072 print_loc_descr (loc, stderr);
6073}
6074
6075/* Print the information collected for a given DIE. */
6076
6077DEBUG_FUNCTION void
6078debug_dwarf_die (dw_die_ref die)
6079{
6080 print_die (die, stderr);
6081}
6082
6083DEBUG_FUNCTION void
6084debug (die_struct &ref)
6085{
6086 print_die (&ref, stderr);
6087}
6088
6089DEBUG_FUNCTION void
6090debug (die_struct *ptr)
6091{
6092 if (ptr)
6093 debug (*ptr);
6094 else
6095 fprintf (stderr, "<nil>\n");
6096}
6097
6098
6099/* Print all DWARF information collected for the compilation unit.
6100 This routine is a debugging aid only. */
6101
6102DEBUG_FUNCTION void
6103debug_dwarf (void)
6104{
6105 print_indent = 0;
6106 print_die (comp_unit_die (), stderr);
6107}
6108
6109/* Verify the DIE tree structure. */
6110
6111DEBUG_FUNCTION void
6112verify_die (dw_die_ref die)
6113{
6114 gcc_assert (!die->die_mark);
6115 if (die->die_parent == NULL
6116 && die->die_sib == NULL)
6117 return;
6118 /* Verify the die_sib list is cyclic. */
6119 dw_die_ref x = die;
6120 do
6121 {
6122 x->die_mark = 1;
6123 x = x->die_sib;
6124 }
6125 while (x && !x->die_mark);
6126 gcc_assert (x == die);
6127 x = die;
6128 do
6129 {
6130 /* Verify all dies have the same parent. */
6131 gcc_assert (x->die_parent == die->die_parent);
6132 if (x->die_child)
6133 {
6134 /* Verify the child has the proper parent and recurse. */
6135 gcc_assert (x->die_child->die_parent == x);
6136 verify_die (x->die_child);
6137 }
6138 x->die_mark = 0;
6139 x = x->die_sib;
6140 }
6141 while (x && x->die_mark);
6142}
6143
6144/* Sanity checks on DIEs. */
6145
6146static void
6147check_die (dw_die_ref die)
6148{
6149 unsigned ix;
6150 dw_attr_node *a;
6151 bool inline_found = false;
6152 int n_location = 0, n_low_pc = 0, n_high_pc = 0, n_artificial = 0;
6153 int n_decl_line = 0, n_decl_column = 0, n_decl_file = 0;
6154 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6155 {
6156 switch (a->dw_attr)
6157 {
6158 case DW_AT_inline:
6159 if (a->dw_attr_val.v.val_unsigned)
6160 inline_found = true;
6161 break;
6162 case DW_AT_location:
6163 ++n_location;
6164 break;
6165 case DW_AT_low_pc:
6166 ++n_low_pc;
6167 break;
6168 case DW_AT_high_pc:
6169 ++n_high_pc;
6170 break;
6171 case DW_AT_artificial:
6172 ++n_artificial;
6173 break;
6174 case DW_AT_decl_column:
6175 ++n_decl_column;
6176 break;
6177 case DW_AT_decl_line:
6178 ++n_decl_line;
6179 break;
6180 case DW_AT_decl_file:
6181 ++n_decl_file;
6182 break;
6183 default:
6184 break;
6185 }
6186 }
6187 if (n_location > 1 || n_low_pc > 1 || n_high_pc > 1 || n_artificial > 1
6188 || n_decl_column > 1 || n_decl_line > 1 || n_decl_file > 1)
6189 {
6190 fprintf (stderr, "Duplicate attributes in DIE:\n");
6191 debug_dwarf_die (die);
6192 gcc_unreachable ();
6193 }
6194 if (inline_found)
6195 {
6196 /* A debugging information entry that is a member of an abstract
6197 instance tree [that has DW_AT_inline] should not contain any
6198 attributes which describe aspects of the subroutine which vary
6199 between distinct inlined expansions or distinct out-of-line
6200 expansions. */
6201 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6202 gcc_assert (a->dw_attr != DW_AT_low_pc
6203 && a->dw_attr != DW_AT_high_pc
6204 && a->dw_attr != DW_AT_location
6205 && a->dw_attr != DW_AT_frame_base
6206 && a->dw_attr != DW_AT_call_all_calls
6207 && a->dw_attr != DW_AT_GNU_all_call_sites);
6208 }
6209}
6210
6211/* Start a new compilation unit DIE for an include file. OLD_UNIT is the CU
6212 for the enclosing include file, if any. BINCL_DIE is the DW_TAG_GNU_BINCL
6213 DIE that marks the start of the DIEs for this include file. */
6214
6215static dw_die_ref
6216push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
6217{
6218 const char *filename = get_AT_string (bincl_die, DW_AT_name);
6219 dw_die_ref new_unit = gen_compile_unit_die (filename);
6220
6221 new_unit->die_sib = old_unit;
6222 return new_unit;
6223}
6224
6225/* Close an include-file CU and reopen the enclosing one. */
6226
6227static dw_die_ref
6228pop_compile_unit (dw_die_ref old_unit)
6229{
6230 dw_die_ref new_unit = old_unit->die_sib;
6231
6232 old_unit->die_sib = NULL;
6233 return new_unit;
6234}
6235
6236#define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
6237#define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
6238#define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
6239
6240/* Calculate the checksum of a location expression. */
6241
6242static inline void
6243loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
6244{
6245 int tem;
6246 inchash::hash hstate;
6247 hashval_t hash;
6248
6249 tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
6250 CHECKSUM (tem);
6251 hash_loc_operands (loc, hstate);
6252 hash = hstate.end();
6253 CHECKSUM (hash);
6254}
6255
6256/* Calculate the checksum of an attribute. */
6257
6258static void
6259attr_checksum (dw_attr_node *at, struct md5_ctx *ctx, int *mark)
6260{
6261 dw_loc_descr_ref loc;
6262 rtx r;
6263
6264 CHECKSUM (at->dw_attr);
6265
6266 /* We don't care that this was compiled with a different compiler
6267 snapshot; if the output is the same, that's what matters. */
6268 if (at->dw_attr == DW_AT_producer)
6269 return;
6270
6271 switch (AT_class (at))
6272 {
6273 case dw_val_class_const:
6274 case dw_val_class_const_implicit:
6275 CHECKSUM (at->dw_attr_val.v.val_int);
6276 break;
6277 case dw_val_class_unsigned_const:
6278 case dw_val_class_unsigned_const_implicit:
6279 CHECKSUM (at->dw_attr_val.v.val_unsigned);
6280 break;
6281 case dw_val_class_const_double:
6282 CHECKSUM (at->dw_attr_val.v.val_double);
6283 break;
6284 case dw_val_class_wide_int:
6285 CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
6286 get_full_len (*at->dw_attr_val.v.val_wide)
6287 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
6288 break;
6289 case dw_val_class_vec:
6290 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
6291 (at->dw_attr_val.v.val_vec.length
6292 * at->dw_attr_val.v.val_vec.elt_size));
6293 break;
6294 case dw_val_class_flag:
6295 CHECKSUM (at->dw_attr_val.v.val_flag);
6296 break;
6297 case dw_val_class_str:
6298 CHECKSUM_STRING (AT_string (at));
6299 break;
6300
6301 case dw_val_class_addr:
6302 r = AT_addr (at);
6303 gcc_assert (GET_CODE (r) == SYMBOL_REF);
6304 CHECKSUM_STRING (XSTR (r, 0));
6305 break;
6306
6307 case dw_val_class_offset:
6308 CHECKSUM (at->dw_attr_val.v.val_offset);
6309 break;
6310
6311 case dw_val_class_loc:
6312 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6313 loc_checksum (loc, ctx);
6314 break;
6315
6316 case dw_val_class_die_ref:
6317 die_checksum (AT_ref (at), ctx, mark);
6318 break;
6319
6320 case dw_val_class_fde_ref:
6321 case dw_val_class_vms_delta:
6322 case dw_val_class_lbl_id:
6323 case dw_val_class_lineptr:
6324 case dw_val_class_macptr:
6325 case dw_val_class_loclistsptr:
6326 case dw_val_class_high_pc:
6327 break;
6328
6329 case dw_val_class_file:
6330 case dw_val_class_file_implicit:
6331 CHECKSUM_STRING (AT_file (at)->filename);
6332 break;
6333
6334 case dw_val_class_data8:
6335 CHECKSUM (at->dw_attr_val.v.val_data8);
6336 break;
6337
6338 default:
6339 break;
6340 }
6341}
6342
6343/* Calculate the checksum of a DIE. */
6344
6345static void
6346die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6347{
6348 dw_die_ref c;
6349 dw_attr_node *a;
6350 unsigned ix;
6351
6352 /* To avoid infinite recursion. */
6353 if (die->die_mark)
6354 {
6355 CHECKSUM (die->die_mark);
6356 return;
6357 }
6358 die->die_mark = ++(*mark);
6359
6360 CHECKSUM (die->die_tag);
6361
6362 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6363 attr_checksum (a, ctx, mark);
6364
6365 FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
6366}
6367
6368#undef CHECKSUM
6369#undef CHECKSUM_BLOCK
6370#undef CHECKSUM_STRING
6371
6372/* For DWARF-4 types, include the trailing NULL when checksumming strings. */
6373#define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
6374#define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
6375#define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
6376#define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
6377#define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
6378#define CHECKSUM_ATTR(FOO) \
6379 if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
6380
6381/* Calculate the checksum of a number in signed LEB128 format. */
6382
6383static void
6384checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
6385{
6386 unsigned char byte;
6387 bool more;
6388
6389 while (1)
6390 {
6391 byte = (value & 0x7f);
6392 value >>= 7;
6393 more = !((value == 0 && (byte & 0x40) == 0)
6394 || (value == -1 && (byte & 0x40) != 0));
6395 if (more)
6396 byte |= 0x80;
6397 CHECKSUM (byte);
6398 if (!more)
6399 break;
6400 }
6401}
6402
6403/* Calculate the checksum of a number in unsigned LEB128 format. */
6404
6405static void
6406checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
6407{
6408 while (1)
6409 {
6410 unsigned char byte = (value & 0x7f);
6411 value >>= 7;
6412 if (value != 0)
6413 /* More bytes to follow. */
6414 byte |= 0x80;
6415 CHECKSUM (byte);
6416 if (value == 0)
6417 break;
6418 }
6419}
6420
6421/* Checksum the context of the DIE. This adds the names of any
6422 surrounding namespaces or structures to the checksum. */
6423
6424static void
6425checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
6426{
6427 const char *name;
6428 dw_die_ref spec;
6429 int tag = die->die_tag;
6430
6431 if (tag != DW_TAG_namespace
6432 && tag != DW_TAG_structure_type
6433 && tag != DW_TAG_class_type)
6434 return;
6435
6436 name = get_AT_string (die, DW_AT_name);
6437
6438 spec = get_AT_ref (die, DW_AT_specification);
6439 if (spec != NULL)
6440 die = spec;
6441
6442 if (die->die_parent != NULL)
6443 checksum_die_context (die->die_parent, ctx);
6444
6445 CHECKSUM_ULEB128 ('C');
6446 CHECKSUM_ULEB128 (tag);
6447 if (name != NULL)
6448 CHECKSUM_STRING (name);
6449}
6450
6451/* Calculate the checksum of a location expression. */
6452
6453static inline void
6454loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
6455{
6456 /* Special case for lone DW_OP_plus_uconst: checksum as if the location
6457 were emitted as a DW_FORM_sdata instead of a location expression. */
6458 if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
6459 {
6460 CHECKSUM_ULEB128 (DW_FORM_sdata);
6461 CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
6462 return;
6463 }
6464
6465 /* Otherwise, just checksum the raw location expression. */
6466 while (loc != NULL)
6467 {
6468 inchash::hash hstate;
6469 hashval_t hash;
6470
6471 CHECKSUM_ULEB128 (loc->dtprel);
6472 CHECKSUM_ULEB128 (loc->dw_loc_opc);
6473 hash_loc_operands (loc, hstate);
6474 hash = hstate.end ();
6475 CHECKSUM (hash);
6476 loc = loc->dw_loc_next;
6477 }
6478}
6479
6480/* Calculate the checksum of an attribute. */
6481
6482static void
6483attr_checksum_ordered (enum dwarf_tag tag, dw_attr_node *at,
6484 struct md5_ctx *ctx, int *mark)
6485{
6486 dw_loc_descr_ref loc;
6487 rtx r;
6488
6489 if (AT_class (at) == dw_val_class_die_ref)
6490 {
6491 dw_die_ref target_die = AT_ref (at);
6492
6493 /* For pointer and reference types, we checksum only the (qualified)
6494 name of the target type (if there is a name). For friend entries,
6495 we checksum only the (qualified) name of the target type or function.
6496 This allows the checksum to remain the same whether the target type
6497 is complete or not. */
6498 if ((at->dw_attr == DW_AT_type
6499 && (tag == DW_TAG_pointer_type
6500 || tag == DW_TAG_reference_type
6501 || tag == DW_TAG_rvalue_reference_type
6502 || tag == DW_TAG_ptr_to_member_type))
6503 || (at->dw_attr == DW_AT_friend
6504 && tag == DW_TAG_friend))
6505 {
6506 dw_attr_node *name_attr = get_AT (target_die, DW_AT_name);
6507
6508 if (name_attr != NULL)
6509 {
6510 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
6511
6512 if (decl == NULL)
6513 decl = target_die;
6514 CHECKSUM_ULEB128 ('N');
6515 CHECKSUM_ULEB128 (at->dw_attr);
6516 if (decl->die_parent != NULL)
6517 checksum_die_context (decl->die_parent, ctx);
6518 CHECKSUM_ULEB128 ('E');
6519 CHECKSUM_STRING (AT_string (name_attr));
6520 return;
6521 }
6522 }
6523
6524 /* For all other references to another DIE, we check to see if the
6525 target DIE has already been visited. If it has, we emit a
6526 backward reference; if not, we descend recursively. */
6527 if (target_die->die_mark > 0)
6528 {
6529 CHECKSUM_ULEB128 ('R');
6530 CHECKSUM_ULEB128 (at->dw_attr);
6531 CHECKSUM_ULEB128 (target_die->die_mark);
6532 }
6533 else
6534 {
6535 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
6536
6537 if (decl == NULL)
6538 decl = target_die;
6539 target_die->die_mark = ++(*mark);
6540 CHECKSUM_ULEB128 ('T');
6541 CHECKSUM_ULEB128 (at->dw_attr);
6542 if (decl->die_parent != NULL)
6543 checksum_die_context (decl->die_parent, ctx);
6544 die_checksum_ordered (target_die, ctx, mark);
6545 }
6546 return;
6547 }
6548
6549 CHECKSUM_ULEB128 ('A');
6550 CHECKSUM_ULEB128 (at->dw_attr);
6551
6552 switch (AT_class (at))
6553 {
6554 case dw_val_class_const:
6555 case dw_val_class_const_implicit:
6556 CHECKSUM_ULEB128 (DW_FORM_sdata);
6557 CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
6558 break;
6559
6560 case dw_val_class_unsigned_const:
6561 case dw_val_class_unsigned_const_implicit:
6562 CHECKSUM_ULEB128 (DW_FORM_sdata);
6563 CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
6564 break;
6565
6566 case dw_val_class_const_double:
6567 CHECKSUM_ULEB128 (DW_FORM_block);
6568 CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
6569 CHECKSUM (at->dw_attr_val.v.val_double);
6570 break;
6571
6572 case dw_val_class_wide_int:
6573 CHECKSUM_ULEB128 (DW_FORM_block);
6574 CHECKSUM_ULEB128 (get_full_len (*at->dw_attr_val.v.val_wide)
6575 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
6576 CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
6577 get_full_len (*at->dw_attr_val.v.val_wide)
6578 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
6579 break;
6580
6581 case dw_val_class_vec:
6582 CHECKSUM_ULEB128 (DW_FORM_block);
6583 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_vec.length
6584 * at->dw_attr_val.v.val_vec.elt_size);
6585 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
6586 (at->dw_attr_val.v.val_vec.length
6587 * at->dw_attr_val.v.val_vec.elt_size));
6588 break;
6589
6590 case dw_val_class_flag:
6591 CHECKSUM_ULEB128 (DW_FORM_flag);
6592 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
6593 break;
6594
6595 case dw_val_class_str:
6596 CHECKSUM_ULEB128 (DW_FORM_string);
6597 CHECKSUM_STRING (AT_string (at));
6598 break;
6599
6600 case dw_val_class_addr:
6601 r = AT_addr (at);
6602 gcc_assert (GET_CODE (r) == SYMBOL_REF);
6603 CHECKSUM_ULEB128 (DW_FORM_string);
6604 CHECKSUM_STRING (XSTR (r, 0));
6605 break;
6606
6607 case dw_val_class_offset:
6608 CHECKSUM_ULEB128 (DW_FORM_sdata);
6609 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
6610 break;
6611
6612 case dw_val_class_loc:
6613 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6614 loc_checksum_ordered (loc, ctx);
6615 break;
6616
6617 case dw_val_class_fde_ref:
6618 case dw_val_class_lbl_id:
6619 case dw_val_class_lineptr:
6620 case dw_val_class_macptr:
6621 case dw_val_class_loclistsptr:
6622 case dw_val_class_high_pc:
6623 break;
6624
6625 case dw_val_class_file:
6626 case dw_val_class_file_implicit:
6627 CHECKSUM_ULEB128 (DW_FORM_string);
6628 CHECKSUM_STRING (AT_file (at)->filename);
6629 break;
6630
6631 case dw_val_class_data8:
6632 CHECKSUM (at->dw_attr_val.v.val_data8);
6633 break;
6634
6635 default:
6636 break;
6637 }
6638}
6639
6640struct checksum_attributes
6641{
6642 dw_attr_node *at_name;
6643 dw_attr_node *at_type;
6644 dw_attr_node *at_friend;
6645 dw_attr_node *at_accessibility;
6646 dw_attr_node *at_address_class;
6647 dw_attr_node *at_alignment;
6648 dw_attr_node *at_allocated;
6649 dw_attr_node *at_artificial;
6650 dw_attr_node *at_associated;
6651 dw_attr_node *at_binary_scale;
6652 dw_attr_node *at_bit_offset;
6653 dw_attr_node *at_bit_size;
6654 dw_attr_node *at_bit_stride;
6655 dw_attr_node *at_byte_size;
6656 dw_attr_node *at_byte_stride;
6657 dw_attr_node *at_const_value;
6658 dw_attr_node *at_containing_type;
6659 dw_attr_node *at_count;
6660 dw_attr_node *at_data_location;
6661 dw_attr_node *at_data_member_location;
6662 dw_attr_node *at_decimal_scale;
6663 dw_attr_node *at_decimal_sign;
6664 dw_attr_node *at_default_value;
6665 dw_attr_node *at_digit_count;
6666 dw_attr_node *at_discr;
6667 dw_attr_node *at_discr_list;
6668 dw_attr_node *at_discr_value;
6669 dw_attr_node *at_encoding;
6670 dw_attr_node *at_endianity;
6671 dw_attr_node *at_explicit;
6672 dw_attr_node *at_is_optional;
6673 dw_attr_node *at_location;
6674 dw_attr_node *at_lower_bound;
6675 dw_attr_node *at_mutable;
6676 dw_attr_node *at_ordering;
6677 dw_attr_node *at_picture_string;
6678 dw_attr_node *at_prototyped;
6679 dw_attr_node *at_small;
6680 dw_attr_node *at_segment;
6681 dw_attr_node *at_string_length;
6682 dw_attr_node *at_string_length_bit_size;
6683 dw_attr_node *at_string_length_byte_size;
6684 dw_attr_node *at_threads_scaled;
6685 dw_attr_node *at_upper_bound;
6686 dw_attr_node *at_use_location;
6687 dw_attr_node *at_use_UTF8;
6688 dw_attr_node *at_variable_parameter;
6689 dw_attr_node *at_virtuality;
6690 dw_attr_node *at_visibility;
6691 dw_attr_node *at_vtable_elem_location;
6692};
6693
6694/* Collect the attributes that we will want to use for the checksum. */
6695
6696static void
6697collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
6698{
6699 dw_attr_node *a;
6700 unsigned ix;
6701
6702 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6703 {
6704 switch (a->dw_attr)
6705 {
6706 case DW_AT_name:
6707 attrs->at_name = a;
6708 break;
6709 case DW_AT_type:
6710 attrs->at_type = a;
6711 break;
6712 case DW_AT_friend:
6713 attrs->at_friend = a;
6714 break;
6715 case DW_AT_accessibility:
6716 attrs->at_accessibility = a;
6717 break;
6718 case DW_AT_address_class:
6719 attrs->at_address_class = a;
6720 break;
6721 case DW_AT_alignment:
6722 attrs->at_alignment = a;
6723 break;
6724 case DW_AT_allocated:
6725 attrs->at_allocated = a;
6726 break;
6727 case DW_AT_artificial:
6728 attrs->at_artificial = a;
6729 break;
6730 case DW_AT_associated:
6731 attrs->at_associated = a;
6732 break;
6733 case DW_AT_binary_scale:
6734 attrs->at_binary_scale = a;
6735 break;
6736 case DW_AT_bit_offset:
6737 attrs->at_bit_offset = a;
6738 break;
6739 case DW_AT_bit_size:
6740 attrs->at_bit_size = a;
6741 break;
6742 case DW_AT_bit_stride:
6743 attrs->at_bit_stride = a;
6744 break;
6745 case DW_AT_byte_size:
6746 attrs->at_byte_size = a;
6747 break;
6748 case DW_AT_byte_stride:
6749 attrs->at_byte_stride = a;
6750 break;
6751 case DW_AT_const_value:
6752 attrs->at_const_value = a;
6753 break;
6754 case DW_AT_containing_type:
6755 attrs->at_containing_type = a;
6756 break;
6757 case DW_AT_count:
6758 attrs->at_count = a;
6759 break;
6760 case DW_AT_data_location:
6761 attrs->at_data_location = a;
6762 break;
6763 case DW_AT_data_member_location:
6764 attrs->at_data_member_location = a;
6765 break;
6766 case DW_AT_decimal_scale:
6767 attrs->at_decimal_scale = a;
6768 break;
6769 case DW_AT_decimal_sign:
6770 attrs->at_decimal_sign = a;
6771 break;
6772 case DW_AT_default_value:
6773 attrs->at_default_value = a;
6774 break;
6775 case DW_AT_digit_count:
6776 attrs->at_digit_count = a;
6777 break;
6778 case DW_AT_discr:
6779 attrs->at_discr = a;
6780 break;
6781 case DW_AT_discr_list:
6782 attrs->at_discr_list = a;
6783 break;
6784 case DW_AT_discr_value:
6785 attrs->at_discr_value = a;
6786 break;
6787 case DW_AT_encoding:
6788 attrs->at_encoding = a;
6789 break;
6790 case DW_AT_endianity:
6791 attrs->at_endianity = a;
6792 break;
6793 case DW_AT_explicit:
6794 attrs->at_explicit = a;
6795 break;
6796 case DW_AT_is_optional:
6797 attrs->at_is_optional = a;
6798 break;
6799 case DW_AT_location:
6800 attrs->at_location = a;
6801 break;
6802 case DW_AT_lower_bound:
6803 attrs->at_lower_bound = a;
6804 break;
6805 case DW_AT_mutable:
6806 attrs->at_mutable = a;
6807 break;
6808 case DW_AT_ordering:
6809 attrs->at_ordering = a;
6810 break;
6811 case DW_AT_picture_string:
6812 attrs->at_picture_string = a;
6813 break;
6814 case DW_AT_prototyped:
6815 attrs->at_prototyped = a;
6816 break;
6817 case DW_AT_small:
6818 attrs->at_small = a;
6819 break;
6820 case DW_AT_segment:
6821 attrs->at_segment = a;
6822 break;
6823 case DW_AT_string_length:
6824 attrs->at_string_length = a;
6825 break;
6826 case DW_AT_string_length_bit_size:
6827 attrs->at_string_length_bit_size = a;
6828 break;
6829 case DW_AT_string_length_byte_size:
6830 attrs->at_string_length_byte_size = a;
6831 break;
6832 case DW_AT_threads_scaled:
6833 attrs->at_threads_scaled = a;
6834 break;
6835 case DW_AT_upper_bound:
6836 attrs->at_upper_bound = a;
6837 break;
6838 case DW_AT_use_location:
6839 attrs->at_use_location = a;
6840 break;
6841 case DW_AT_use_UTF8:
6842 attrs->at_use_UTF8 = a;
6843 break;
6844 case DW_AT_variable_parameter:
6845 attrs->at_variable_parameter = a;
6846 break;
6847 case DW_AT_virtuality:
6848 attrs->at_virtuality = a;
6849 break;
6850 case DW_AT_visibility:
6851 attrs->at_visibility = a;
6852 break;
6853 case DW_AT_vtable_elem_location:
6854 attrs->at_vtable_elem_location = a;
6855 break;
6856 default:
6857 break;
6858 }
6859 }
6860}
6861
6862/* Calculate the checksum of a DIE, using an ordered subset of attributes. */
6863
6864static void
6865die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6866{
6867 dw_die_ref c;
6868 dw_die_ref decl;
6869 struct checksum_attributes attrs;
6870
6871 CHECKSUM_ULEB128 ('D');
6872 CHECKSUM_ULEB128 (die->die_tag);
6873
6874 memset (&attrs, 0, sizeof (attrs));
6875
6876 decl = get_AT_ref (die, DW_AT_specification);
6877 if (decl != NULL)
6878 collect_checksum_attributes (&attrs, decl);
6879 collect_checksum_attributes (&attrs, die);
6880
6881 CHECKSUM_ATTR (attrs.at_name);
6882 CHECKSUM_ATTR (attrs.at_accessibility);
6883 CHECKSUM_ATTR (attrs.at_address_class);
6884 CHECKSUM_ATTR (attrs.at_allocated);
6885 CHECKSUM_ATTR (attrs.at_artificial);
6886 CHECKSUM_ATTR (attrs.at_associated);
6887 CHECKSUM_ATTR (attrs.at_binary_scale);
6888 CHECKSUM_ATTR (attrs.at_bit_offset);
6889 CHECKSUM_ATTR (attrs.at_bit_size);
6890 CHECKSUM_ATTR (attrs.at_bit_stride);
6891 CHECKSUM_ATTR (attrs.at_byte_size);
6892 CHECKSUM_ATTR (attrs.at_byte_stride);
6893 CHECKSUM_ATTR (attrs.at_const_value);
6894 CHECKSUM_ATTR (attrs.at_containing_type);
6895 CHECKSUM_ATTR (attrs.at_count);
6896 CHECKSUM_ATTR (attrs.at_data_location);
6897 CHECKSUM_ATTR (attrs.at_data_member_location);
6898 CHECKSUM_ATTR (attrs.at_decimal_scale);
6899 CHECKSUM_ATTR (attrs.at_decimal_sign);
6900 CHECKSUM_ATTR (attrs.at_default_value);
6901 CHECKSUM_ATTR (attrs.at_digit_count);
6902 CHECKSUM_ATTR (attrs.at_discr);
6903 CHECKSUM_ATTR (attrs.at_discr_list);
6904 CHECKSUM_ATTR (attrs.at_discr_value);
6905 CHECKSUM_ATTR (attrs.at_encoding);
6906 CHECKSUM_ATTR (attrs.at_endianity);
6907 CHECKSUM_ATTR (attrs.at_explicit);
6908 CHECKSUM_ATTR (attrs.at_is_optional);
6909 CHECKSUM_ATTR (attrs.at_location);
6910 CHECKSUM_ATTR (attrs.at_lower_bound);
6911 CHECKSUM_ATTR (attrs.at_mutable);
6912 CHECKSUM_ATTR (attrs.at_ordering);
6913 CHECKSUM_ATTR (attrs.at_picture_string);
6914 CHECKSUM_ATTR (attrs.at_prototyped);
6915 CHECKSUM_ATTR (attrs.at_small);
6916 CHECKSUM_ATTR (attrs.at_segment);
6917 CHECKSUM_ATTR (attrs.at_string_length);
6918 CHECKSUM_ATTR (attrs.at_string_length_bit_size);
6919 CHECKSUM_ATTR (attrs.at_string_length_byte_size);
6920 CHECKSUM_ATTR (attrs.at_threads_scaled);
6921 CHECKSUM_ATTR (attrs.at_upper_bound);
6922 CHECKSUM_ATTR (attrs.at_use_location);
6923 CHECKSUM_ATTR (attrs.at_use_UTF8);
6924 CHECKSUM_ATTR (attrs.at_variable_parameter);
6925 CHECKSUM_ATTR (attrs.at_virtuality);
6926 CHECKSUM_ATTR (attrs.at_visibility);
6927 CHECKSUM_ATTR (attrs.at_vtable_elem_location);
6928 CHECKSUM_ATTR (attrs.at_type);
6929 CHECKSUM_ATTR (attrs.at_friend);
6930 CHECKSUM_ATTR (attrs.at_alignment);
6931
6932 /* Checksum the child DIEs. */
6933 c = die->die_child;
6934 if (c) do {
6935 dw_attr_node *name_attr;
6936
6937 c = c->die_sib;
6938 name_attr = get_AT (c, DW_AT_name);
6939 if (is_template_instantiation (c))
6940 {
6941 /* Ignore instantiations of member type and function templates. */
6942 }
6943 else if (name_attr != NULL
6944 && (is_type_die (c) || c->die_tag == DW_TAG_subprogram))
6945 {
6946 /* Use a shallow checksum for named nested types and member
6947 functions. */
6948 CHECKSUM_ULEB128 ('S');
6949 CHECKSUM_ULEB128 (c->die_tag);
6950 CHECKSUM_STRING (AT_string (name_attr));
6951 }
6952 else
6953 {
6954 /* Use a deep checksum for other children. */
6955 /* Mark this DIE so it gets processed when unmarking. */
6956 if (c->die_mark == 0)
6957 c->die_mark = -1;
6958 die_checksum_ordered (c, ctx, mark);
6959 }
6960 } while (c != die->die_child);
6961
6962 CHECKSUM_ULEB128 (0);
6963}
6964
6965/* Add a type name and tag to a hash. */
6966static void
6967die_odr_checksum (int tag, const char *name, md5_ctx *ctx)
6968{
6969 CHECKSUM_ULEB128 (tag);
6970 CHECKSUM_STRING (name);
6971}
6972
6973#undef CHECKSUM
6974#undef CHECKSUM_STRING
6975#undef CHECKSUM_ATTR
6976#undef CHECKSUM_LEB128
6977#undef CHECKSUM_ULEB128
6978
6979/* Generate the type signature for DIE. This is computed by generating an
6980 MD5 checksum over the DIE's tag, its relevant attributes, and its
6981 children. Attributes that are references to other DIEs are processed
6982 by recursion, using the MARK field to prevent infinite recursion.
6983 If the DIE is nested inside a namespace or another type, we also
6984 need to include that context in the signature. The lower 64 bits
6985 of the resulting MD5 checksum comprise the signature. */
6986
6987static void
6988generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
6989{
6990 int mark;
6991 const char *name;
6992 unsigned char checksum[16];
6993 struct md5_ctx ctx;
6994 dw_die_ref decl;
6995 dw_die_ref parent;
6996
6997 name = get_AT_string (die, DW_AT_name);
6998 decl = get_AT_ref (die, DW_AT_specification);
6999 parent = get_die_parent (die);
7000
7001 /* First, compute a signature for just the type name (and its surrounding
7002 context, if any. This is stored in the type unit DIE for link-time
7003 ODR (one-definition rule) checking. */
7004
7005 if (is_cxx () && name != NULL)
7006 {
7007 md5_init_ctx (&ctx);
7008
7009 /* Checksum the names of surrounding namespaces and structures. */
7010 if (parent != NULL)
7011 checksum_die_context (parent, &ctx);
7012
7013 /* Checksum the current DIE. */
7014 die_odr_checksum (die->die_tag, name, &ctx);
7015 md5_finish_ctx (&ctx, checksum);
7016
7017 add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
7018 }
7019
7020 /* Next, compute the complete type signature. */
7021
7022 md5_init_ctx (&ctx);
7023 mark = 1;
7024 die->die_mark = mark;
7025
7026 /* Checksum the names of surrounding namespaces and structures. */
7027 if (parent != NULL)
7028 checksum_die_context (parent, &ctx);
7029
7030 /* Checksum the DIE and its children. */
7031 die_checksum_ordered (die, &ctx, &mark);
7032 unmark_all_dies (die);
7033 md5_finish_ctx (&ctx, checksum);
7034
7035 /* Store the signature in the type node and link the type DIE and the
7036 type node together. */
7037 memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
7038 DWARF_TYPE_SIGNATURE_SIZE);
7039 die->comdat_type_p = true;
7040 die->die_id.die_type_node = type_node;
7041 type_node->type_die = die;
7042
7043 /* If the DIE is a specification, link its declaration to the type node
7044 as well. */
7045 if (decl != NULL)
7046 {
7047 decl->comdat_type_p = true;
7048 decl->die_id.die_type_node = type_node;
7049 }
7050}
7051
7052/* Do the location expressions look same? */
7053static inline int
7054same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
7055{
7056 return loc1->dw_loc_opc == loc2->dw_loc_opc
7057 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
7058 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
7059}
7060
7061/* Do the values look the same? */
7062static int
7063same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
7064{
7065 dw_loc_descr_ref loc1, loc2;
7066 rtx r1, r2;
7067
7068 if (v1->val_class != v2->val_class)
7069 return 0;
7070
7071 switch (v1->val_class)
7072 {
7073 case dw_val_class_const:
7074 case dw_val_class_const_implicit:
7075 return v1->v.val_int == v2->v.val_int;
7076 case dw_val_class_unsigned_const:
7077 case dw_val_class_unsigned_const_implicit:
7078 return v1->v.val_unsigned == v2->v.val_unsigned;
7079 case dw_val_class_const_double:
7080 return v1->v.val_double.high == v2->v.val_double.high
7081 && v1->v.val_double.low == v2->v.val_double.low;
7082 case dw_val_class_wide_int:
7083 return *v1->v.val_wide == *v2->v.val_wide;
7084 case dw_val_class_vec:
7085 if (v1->v.val_vec.length != v2->v.val_vec.length
7086 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
7087 return 0;
7088 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
7089 v1->v.val_vec.length * v1->v.val_vec.elt_size))
7090 return 0;
7091 return 1;
7092 case dw_val_class_flag:
7093 return v1->v.val_flag == v2->v.val_flag;
7094 case dw_val_class_str:
7095 return !strcmp (v1->v.val_str->str, v2->v.val_str->str);
7096
7097 case dw_val_class_addr:
7098 r1 = v1->v.val_addr;
7099 r2 = v2->v.val_addr;
7100 if (GET_CODE (r1) != GET_CODE (r2))
7101 return 0;
7102 return !rtx_equal_p (r1, r2);
7103
7104 case dw_val_class_offset:
7105 return v1->v.val_offset == v2->v.val_offset;
7106
7107 case dw_val_class_loc:
7108 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
7109 loc1 && loc2;
7110 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
7111 if (!same_loc_p (loc1, loc2, mark))
7112 return 0;
7113 return !loc1 && !loc2;
7114
7115 case dw_val_class_die_ref:
7116 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
7117
7118 case dw_val_class_fde_ref:
7119 case dw_val_class_vms_delta:
7120 case dw_val_class_lbl_id:
7121 case dw_val_class_lineptr:
7122 case dw_val_class_macptr:
7123 case dw_val_class_loclistsptr:
7124 case dw_val_class_high_pc:
7125 return 1;
7126
7127 case dw_val_class_file:
7128 case dw_val_class_file_implicit:
7129 return v1->v.val_file == v2->v.val_file;
7130
7131 case dw_val_class_data8:
7132 return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
7133
7134 default:
7135 return 1;
7136 }
7137}
7138
7139/* Do the attributes look the same? */
7140
7141static int
7142same_attr_p (dw_attr_node *at1, dw_attr_node *at2, int *mark)
7143{
7144 if (at1->dw_attr != at2->dw_attr)
7145 return 0;
7146
7147 /* We don't care that this was compiled with a different compiler
7148 snapshot; if the output is the same, that's what matters. */
7149 if (at1->dw_attr == DW_AT_producer)
7150 return 1;
7151
7152 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
7153}
7154
7155/* Do the dies look the same? */
7156
7157static int
7158same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
7159{
7160 dw_die_ref c1, c2;
7161 dw_attr_node *a1;
7162 unsigned ix;
7163
7164 /* To avoid infinite recursion. */
7165 if (die1->die_mark)
7166 return die1->die_mark == die2->die_mark;
7167 die1->die_mark = die2->die_mark = ++(*mark);
7168
7169 if (die1->die_tag != die2->die_tag)
7170 return 0;
7171
7172 if (vec_safe_length (die1->die_attr) != vec_safe_length (die2->die_attr))
7173 return 0;
7174
7175 FOR_EACH_VEC_SAFE_ELT (die1->die_attr, ix, a1)
7176 if (!same_attr_p (a1, &(*die2->die_attr)[ix], mark))
7177 return 0;
7178
7179 c1 = die1->die_child;
7180 c2 = die2->die_child;
7181 if (! c1)
7182 {
7183 if (c2)
7184 return 0;
7185 }
7186 else
7187 for (;;)
7188 {
7189 if (!same_die_p (c1, c2, mark))
7190 return 0;
7191 c1 = c1->die_sib;
7192 c2 = c2->die_sib;
7193 if (c1 == die1->die_child)
7194 {
7195 if (c2 == die2->die_child)
7196 break;
7197 else
7198 return 0;
7199 }
7200 }
7201
7202 return 1;
7203}
7204
7205/* Do the dies look the same? Wrapper around same_die_p. */
7206
7207static int
7208same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
7209{
7210 int mark = 0;
7211 int ret = same_die_p (die1, die2, &mark);
7212
7213 unmark_all_dies (die1);
7214 unmark_all_dies (die2);
7215
7216 return ret;
7217}
7218
7219/* The prefix to attach to symbols on DIEs in the current comdat debug
7220 info section. */
7221static const char *comdat_symbol_id;
7222
7223/* The index of the current symbol within the current comdat CU. */
7224static unsigned int comdat_symbol_number;
7225
7226/* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
7227 children, and set comdat_symbol_id accordingly. */
7228
7229static void
7230compute_section_prefix (dw_die_ref unit_die)
7231{
7232 const char *die_name = get_AT_string (unit_die, DW_AT_name);
7233 const char *base = die_name ? lbasename (die_name) : "anonymous";
7234 char *name = XALLOCAVEC (char, strlen (base) + 64);
7235 char *p;
7236 int i, mark;
7237 unsigned char checksum[16];
7238 struct md5_ctx ctx;
7239
7240 /* Compute the checksum of the DIE, then append part of it as hex digits to
7241 the name filename of the unit. */
7242
7243 md5_init_ctx (&ctx);
7244 mark = 0;
7245 die_checksum (unit_die, &ctx, &mark);
7246 unmark_all_dies (unit_die);
7247 md5_finish_ctx (&ctx, checksum);
7248
7249 sprintf (name, "%s.", base);
7250 clean_symbol_name (name);
7251
7252 p = name + strlen (name);
7253 for (i = 0; i < 4; i++)
7254 {
7255 sprintf (p, "%.2x", checksum[i]);
7256 p += 2;
7257 }
7258
7259 comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
7260 comdat_symbol_number = 0;
7261}
7262
7263/* Returns nonzero if DIE represents a type, in the sense of TYPE_P. */
7264
7265static int
7266is_type_die (dw_die_ref die)
7267{
7268 switch (die->die_tag)
7269 {
7270 case DW_TAG_array_type:
7271 case DW_TAG_class_type:
7272 case DW_TAG_interface_type:
7273 case DW_TAG_enumeration_type:
7274 case DW_TAG_pointer_type:
7275 case DW_TAG_reference_type:
7276 case DW_TAG_rvalue_reference_type:
7277 case DW_TAG_string_type:
7278 case DW_TAG_structure_type:
7279 case DW_TAG_subroutine_type:
7280 case DW_TAG_union_type:
7281 case DW_TAG_ptr_to_member_type:
7282 case DW_TAG_set_type:
7283 case DW_TAG_subrange_type:
7284 case DW_TAG_base_type:
7285 case DW_TAG_const_type:
7286 case DW_TAG_file_type:
7287 case DW_TAG_packed_type:
7288 case DW_TAG_volatile_type:
7289 case DW_TAG_typedef:
7290 return 1;
7291 default:
7292 return 0;
7293 }
7294}
7295
7296/* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
7297 Basically, we want to choose the bits that are likely to be shared between
7298 compilations (types) and leave out the bits that are specific to individual
7299 compilations (functions). */
7300
7301static int
7302is_comdat_die (dw_die_ref c)
7303{
7304 /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
7305 we do for stabs. The advantage is a greater likelihood of sharing between
7306 objects that don't include headers in the same order (and therefore would
7307 put the base types in a different comdat). jason 8/28/00 */
7308
7309 if (c->die_tag == DW_TAG_base_type)
7310 return 0;
7311
7312 if (c->die_tag == DW_TAG_pointer_type
7313 || c->die_tag == DW_TAG_reference_type
7314 || c->die_tag == DW_TAG_rvalue_reference_type
7315 || c->die_tag == DW_TAG_const_type
7316 || c->die_tag == DW_TAG_volatile_type)
7317 {
7318 dw_die_ref t = get_AT_ref (c, DW_AT_type);
7319
7320 return t ? is_comdat_die (t) : 0;
7321 }
7322
7323 return is_type_die (c);
7324}
7325
7326/* Returns 1 iff C is the sort of DIE that might be referred to from another
7327 compilation unit. */
7328
7329static int
7330is_symbol_die (dw_die_ref c)
7331{
7332 return (is_type_die (c)
7333 || is_declaration_die (c)
7334 || c->die_tag == DW_TAG_namespace
7335 || c->die_tag == DW_TAG_module);
7336}
7337
7338/* Returns true iff C is a compile-unit DIE. */
7339
7340static inline bool
7341is_cu_die (dw_die_ref c)
7342{
7343 return c && (c->die_tag == DW_TAG_compile_unit
7344 || c->die_tag == DW_TAG_skeleton_unit);
7345}
7346
7347/* Returns true iff C is a unit DIE of some sort. */
7348
7349static inline bool
7350is_unit_die (dw_die_ref c)
7351{
7352 return c && (c->die_tag == DW_TAG_compile_unit
7353 || c->die_tag == DW_TAG_partial_unit
7354 || c->die_tag == DW_TAG_type_unit
7355 || c->die_tag == DW_TAG_skeleton_unit);
7356}
7357
7358/* Returns true iff C is a namespace DIE. */
7359
7360static inline bool
7361is_namespace_die (dw_die_ref c)
7362{
7363 return c && c->die_tag == DW_TAG_namespace;
7364}
7365
7366/* Returns true iff C is a class or structure DIE. */
7367
7368static inline bool
7369is_class_die (dw_die_ref c)
7370{
7371 return c && (c->die_tag == DW_TAG_class_type
7372 || c->die_tag == DW_TAG_structure_type);
7373}
7374
7375/* Return non-zero if this DIE is a template parameter. */
7376
7377static inline bool
7378is_template_parameter (dw_die_ref die)
7379{
7380 switch (die->die_tag)
7381 {
7382 case DW_TAG_template_type_param:
7383 case DW_TAG_template_value_param:
7384 case DW_TAG_GNU_template_template_param:
7385 case DW_TAG_GNU_template_parameter_pack:
7386 return true;
7387 default:
7388 return false;
7389 }
7390}
7391
7392/* Return non-zero if this DIE represents a template instantiation. */
7393
7394static inline bool
7395is_template_instantiation (dw_die_ref die)
7396{
7397 dw_die_ref c;
7398
7399 if (!is_type_die (die) && die->die_tag != DW_TAG_subprogram)
7400 return false;
7401 FOR_EACH_CHILD (die, c, if (is_template_parameter (c)) return true);
7402 return false;
7403}
7404
7405static char *
7406gen_internal_sym (const char *prefix)
7407{
7408 char buf[MAX_ARTIFICIAL_LABEL_BYTES];
7409
7410 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
7411 return xstrdup (buf);
7412}
7413
7414/* Assign symbols to all worthy DIEs under DIE. */
7415
7416static void
7417assign_symbol_names (dw_die_ref die)
7418{
7419 dw_die_ref c;
7420
7421 if (is_symbol_die (die) && !die->comdat_type_p)
7422 {
7423 if (comdat_symbol_id)
7424 {
7425 char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
7426
7427 sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
7428 comdat_symbol_id, comdat_symbol_number++);
7429 die->die_id.die_symbol = xstrdup (p);
7430 }
7431 else
7432 die->die_id.die_symbol = gen_internal_sym ("LDIE");
7433 }
7434
7435 FOR_EACH_CHILD (die, c, assign_symbol_names (c));
7436}
7437
7438struct cu_hash_table_entry
7439{
7440 dw_die_ref cu;
7441 unsigned min_comdat_num, max_comdat_num;
7442 struct cu_hash_table_entry *next;
7443};
7444
7445/* Helpers to manipulate hash table of CUs. */
7446
7447struct cu_hash_table_entry_hasher : pointer_hash <cu_hash_table_entry>
7448{
7449 typedef die_struct *compare_type;
7450 static inline hashval_t hash (const cu_hash_table_entry *);
7451 static inline bool equal (const cu_hash_table_entry *, const die_struct *);
7452 static inline void remove (cu_hash_table_entry *);
7453};
7454
7455inline hashval_t
7456cu_hash_table_entry_hasher::hash (const cu_hash_table_entry *entry)
7457{
7458 return htab_hash_string (entry->cu->die_id.die_symbol);
7459}
7460
7461inline bool
7462cu_hash_table_entry_hasher::equal (const cu_hash_table_entry *entry1,
7463 const die_struct *entry2)
7464{
7465 return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
7466}
7467
7468inline void
7469cu_hash_table_entry_hasher::remove (cu_hash_table_entry *entry)
7470{
7471 struct cu_hash_table_entry *next;
7472
7473 while (entry)
7474 {
7475 next = entry->next;
7476 free (entry);
7477 entry = next;
7478 }
7479}
7480
7481typedef hash_table<cu_hash_table_entry_hasher> cu_hash_type;
7482
7483/* Check whether we have already seen this CU and set up SYM_NUM
7484 accordingly. */
7485static int
7486check_duplicate_cu (dw_die_ref cu, cu_hash_type *htable, unsigned int *sym_num)
7487{
7488 struct cu_hash_table_entry dummy;
7489 struct cu_hash_table_entry **slot, *entry, *last = &dummy;
7490
7491 dummy.max_comdat_num = 0;
7492
7493 slot = htable->find_slot_with_hash (cu,
7494 htab_hash_string (cu->die_id.die_symbol),
7495 INSERT);
7496 entry = *slot;
7497
7498 for (; entry; last = entry, entry = entry->next)
7499 {
7500 if (same_die_p_wrap (cu, entry->cu))
7501 break;
7502 }
7503
7504 if (entry)
7505 {
7506 *sym_num = entry->min_comdat_num;
7507 return 1;
7508 }
7509
7510 entry = XCNEW (struct cu_hash_table_entry);
7511 entry->cu = cu;
7512 entry->min_comdat_num = *sym_num = last->max_comdat_num;
7513 entry->next = *slot;
7514 *slot = entry;
7515
7516 return 0;
7517}
7518
7519/* Record SYM_NUM to record of CU in HTABLE. */
7520static void
7521record_comdat_symbol_number (dw_die_ref cu, cu_hash_type *htable,
7522 unsigned int sym_num)
7523{
7524 struct cu_hash_table_entry **slot, *entry;
7525
7526 slot = htable->find_slot_with_hash (cu,
7527 htab_hash_string (cu->die_id.die_symbol),
7528 NO_INSERT);
7529 entry = *slot;
7530
7531 entry->max_comdat_num = sym_num;
7532}
7533
7534/* Traverse the DIE (which is always comp_unit_die), and set up
7535 additional compilation units for each of the include files we see
7536 bracketed by BINCL/EINCL. */
7537
7538static void
7539break_out_includes (dw_die_ref die)
7540{
7541 dw_die_ref c;
7542 dw_die_ref unit = NULL;
7543 limbo_die_node *node, **pnode;
7544
7545 c = die->die_child;
7546 if (c) do {
7547 dw_die_ref prev = c;
7548 c = c->die_sib;
7549 while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
7550 || (unit && is_comdat_die (c)))
7551 {
7552 dw_die_ref next = c->die_sib;
7553
7554 /* This DIE is for a secondary CU; remove it from the main one. */
7555 remove_child_with_prev (c, prev);
7556
7557 if (c->die_tag == DW_TAG_GNU_BINCL)
7558 unit = push_new_compile_unit (unit, c);
7559 else if (c->die_tag == DW_TAG_GNU_EINCL)
7560 unit = pop_compile_unit (unit);
7561 else
7562 add_child_die (unit, c);
7563 c = next;
7564 if (c == die->die_child)
7565 break;
7566 }
7567 } while (c != die->die_child);
7568
7569#if 0
7570 /* We can only use this in debugging, since the frontend doesn't check
7571 to make sure that we leave every include file we enter. */
7572 gcc_assert (!unit);
7573#endif
7574
7575 assign_symbol_names (die);
7576 cu_hash_type cu_hash_table (10);
7577 for (node = limbo_die_list, pnode = &limbo_die_list;
7578 node;
7579 node = node->next)
7580 {
7581 int is_dupl;
7582
7583 compute_section_prefix (node->die);
7584 is_dupl = check_duplicate_cu (node->die, &cu_hash_table,
7585 &comdat_symbol_number);
7586 assign_symbol_names (node->die);
7587 if (is_dupl)
7588 *pnode = node->next;
7589 else
7590 {
7591 pnode = &node->next;
7592 record_comdat_symbol_number (node->die, &cu_hash_table,
7593 comdat_symbol_number);
7594 }
7595 }
7596}
7597
7598/* Return non-zero if this DIE is a declaration. */
7599
7600static int
7601is_declaration_die (dw_die_ref die)
7602{
7603 dw_attr_node *a;
7604 unsigned ix;
7605
7606 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7607 if (a->dw_attr == DW_AT_declaration)
7608 return 1;
7609
7610 return 0;
7611}
7612
7613/* Return non-zero if this DIE is nested inside a subprogram. */
7614
7615static int
7616is_nested_in_subprogram (dw_die_ref die)
7617{
7618 dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
7619
7620 if (decl == NULL)
7621 decl = die;
7622 return local_scope_p (decl);
7623}
7624
7625/* Return non-zero if this DIE contains a defining declaration of a
7626 subprogram. */
7627
7628static int
7629contains_subprogram_definition (dw_die_ref die)
7630{
7631 dw_die_ref c;
7632
7633 if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
7634 return 1;
7635 FOR_EACH_CHILD (die, c, if (contains_subprogram_definition (c)) return 1);
7636 return 0;
7637}
7638
7639/* Return non-zero if this is a type DIE that should be moved to a
7640 COMDAT .debug_types section or .debug_info section with DW_UT_*type
7641 unit type. */
7642
7643static int
7644should_move_die_to_comdat (dw_die_ref die)
7645{
7646 switch (die->die_tag)
7647 {
7648 case DW_TAG_class_type:
7649 case DW_TAG_structure_type:
7650 case DW_TAG_enumeration_type:
7651 case DW_TAG_union_type:
7652 /* Don't move declarations, inlined instances, types nested in a
7653 subprogram, or types that contain subprogram definitions. */
7654 if (is_declaration_die (die)
7655 || get_AT (die, DW_AT_abstract_origin)
7656 || is_nested_in_subprogram (die)
7657 || contains_subprogram_definition (die))
7658 return 0;
7659 return 1;
7660 case DW_TAG_array_type:
7661 case DW_TAG_interface_type:
7662 case DW_TAG_pointer_type:
7663 case DW_TAG_reference_type:
7664 case DW_TAG_rvalue_reference_type:
7665 case DW_TAG_string_type:
7666 case DW_TAG_subroutine_type:
7667 case DW_TAG_ptr_to_member_type:
7668 case DW_TAG_set_type:
7669 case DW_TAG_subrange_type:
7670 case DW_TAG_base_type:
7671 case DW_TAG_const_type:
7672 case DW_TAG_file_type:
7673 case DW_TAG_packed_type:
7674 case DW_TAG_volatile_type:
7675 case DW_TAG_typedef:
7676 default:
7677 return 0;
7678 }
7679}
7680
7681/* Make a clone of DIE. */
7682
7683static dw_die_ref
7684clone_die (dw_die_ref die)
7685{
7686 dw_die_ref clone;
7687 dw_attr_node *a;
7688 unsigned ix;
7689
7690 clone = ggc_cleared_alloc<die_node> ();
7691 clone->die_tag = die->die_tag;
7692
7693 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7694 add_dwarf_attr (clone, a);
7695
7696 return clone;
7697}
7698
7699/* Make a clone of the tree rooted at DIE. */
7700
7701static dw_die_ref
7702clone_tree (dw_die_ref die)
7703{
7704 dw_die_ref c;
7705 dw_die_ref clone = clone_die (die);
7706
7707 FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree (c)));
7708
7709 return clone;
7710}
7711
7712/* Make a clone of DIE as a declaration. */
7713
7714static dw_die_ref
7715clone_as_declaration (dw_die_ref die)
7716{
7717 dw_die_ref clone;
7718 dw_die_ref decl;
7719 dw_attr_node *a;
7720 unsigned ix;
7721
7722 /* If the DIE is already a declaration, just clone it. */
7723 if (is_declaration_die (die))
7724 return clone_die (die);
7725
7726 /* If the DIE is a specification, just clone its declaration DIE. */
7727 decl = get_AT_ref (die, DW_AT_specification);
7728 if (decl != NULL)
7729 {
7730 clone = clone_die (decl);
7731 if (die->comdat_type_p)
7732 add_AT_die_ref (clone, DW_AT_signature, die);
7733 return clone;
7734 }
7735
7736 clone = ggc_cleared_alloc<die_node> ();
7737 clone->die_tag = die->die_tag;
7738
7739 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7740 {
7741 /* We don't want to copy over all attributes.
7742 For example we don't want DW_AT_byte_size because otherwise we will no
7743 longer have a declaration and GDB will treat it as a definition. */
7744
7745 switch (a->dw_attr)
7746 {
7747 case DW_AT_abstract_origin:
7748 case DW_AT_artificial:
7749 case DW_AT_containing_type:
7750 case DW_AT_external:
7751 case DW_AT_name:
7752 case DW_AT_type:
7753 case DW_AT_virtuality:
7754 case DW_AT_linkage_name:
7755 case DW_AT_MIPS_linkage_name:
7756 add_dwarf_attr (clone, a);
7757 break;
7758 case DW_AT_byte_size:
7759 case DW_AT_alignment:
7760 default:
7761 break;
7762 }
7763 }
7764
7765 if (die->comdat_type_p)
7766 add_AT_die_ref (clone, DW_AT_signature, die);
7767
7768 add_AT_flag (clone, DW_AT_declaration, 1);
7769 return clone;
7770}
7771
7772
7773/* Structure to map a DIE in one CU to its copy in a comdat type unit. */
7774
7775struct decl_table_entry
7776{
7777 dw_die_ref orig;
7778 dw_die_ref copy;
7779};
7780
7781/* Helpers to manipulate hash table of copied declarations. */
7782
7783/* Hashtable helpers. */
7784
7785struct decl_table_entry_hasher : free_ptr_hash <decl_table_entry>
7786{
7787 typedef die_struct *compare_type;
7788 static inline hashval_t hash (const decl_table_entry *);
7789 static inline bool equal (const decl_table_entry *, const die_struct *);
7790};
7791
7792inline hashval_t
7793decl_table_entry_hasher::hash (const decl_table_entry *entry)
7794{
7795 return htab_hash_pointer (entry->orig);
7796}
7797
7798inline bool
7799decl_table_entry_hasher::equal (const decl_table_entry *entry1,
7800 const die_struct *entry2)
7801{
7802 return entry1->orig == entry2;
7803}
7804
7805typedef hash_table<decl_table_entry_hasher> decl_hash_type;
7806
7807/* Copy DIE and its ancestors, up to, but not including, the compile unit
7808 or type unit entry, to a new tree. Adds the new tree to UNIT and returns
7809 a pointer to the copy of DIE. If DECL_TABLE is provided, it is used
7810 to check if the ancestor has already been copied into UNIT. */
7811
7812static dw_die_ref
7813copy_ancestor_tree (dw_die_ref unit, dw_die_ref die,
7814 decl_hash_type *decl_table)
7815{
7816 dw_die_ref parent = die->die_parent;
7817 dw_die_ref new_parent = unit;
7818 dw_die_ref copy;
7819 decl_table_entry **slot = NULL;
7820 struct decl_table_entry *entry = NULL;
7821
7822 if (decl_table)
7823 {
7824 /* Check if the entry has already been copied to UNIT. */
7825 slot = decl_table->find_slot_with_hash (die, htab_hash_pointer (die),
7826 INSERT);
7827 if (*slot != HTAB_EMPTY_ENTRY)
7828 {
7829 entry = *slot;
7830 return entry->copy;
7831 }
7832
7833 /* Record in DECL_TABLE that DIE has been copied to UNIT. */
7834 entry = XCNEW (struct decl_table_entry);
7835 entry->orig = die;
7836 entry->copy = NULL;
7837 *slot = entry;
7838 }
7839
7840 if (parent != NULL)
7841 {
7842 dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
7843 if (spec != NULL)
7844 parent = spec;
7845 if (!is_unit_die (parent))
7846 new_parent = copy_ancestor_tree (unit, parent, decl_table);
7847 }
7848
7849 copy = clone_as_declaration (die);
7850 add_child_die (new_parent, copy);
7851
7852 if (decl_table)
7853 {
7854 /* Record the pointer to the copy. */
7855 entry->copy = copy;
7856 }
7857
7858 return copy;
7859}
7860/* Copy the declaration context to the new type unit DIE. This includes
7861 any surrounding namespace or type declarations. If the DIE has an
7862 AT_specification attribute, it also includes attributes and children
7863 attached to the specification, and returns a pointer to the original
7864 parent of the declaration DIE. Returns NULL otherwise. */
7865
7866static dw_die_ref
7867copy_declaration_context (dw_die_ref unit, dw_die_ref die)
7868{
7869 dw_die_ref decl;
7870 dw_die_ref new_decl;
7871 dw_die_ref orig_parent = NULL;
7872
7873 decl = get_AT_ref (die, DW_AT_specification);
7874 if (decl == NULL)
7875 decl = die;
7876 else
7877 {
7878 unsigned ix;
7879 dw_die_ref c;
7880 dw_attr_node *a;
7881
7882 /* The original DIE will be changed to a declaration, and must
7883 be moved to be a child of the original declaration DIE. */
7884 orig_parent = decl->die_parent;
7885
7886 /* Copy the type node pointer from the new DIE to the original
7887 declaration DIE so we can forward references later. */
7888 decl->comdat_type_p = true;
7889 decl->die_id.die_type_node = die->die_id.die_type_node;
7890
7891 remove_AT (die, DW_AT_specification);
7892
7893 FOR_EACH_VEC_SAFE_ELT (decl->die_attr, ix, a)
7894 {
7895 if (a->dw_attr != DW_AT_name
7896 && a->dw_attr != DW_AT_declaration
7897 && a->dw_attr != DW_AT_external)
7898 add_dwarf_attr (die, a);
7899 }
7900
7901 FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree (c)));
7902 }
7903
7904 if (decl->die_parent != NULL
7905 && !is_unit_die (decl->die_parent))
7906 {
7907 new_decl = copy_ancestor_tree (unit, decl, NULL);
7908 if (new_decl != NULL)
7909 {
7910 remove_AT (new_decl, DW_AT_signature);
7911 add_AT_specification (die, new_decl);
7912 }
7913 }
7914
7915 return orig_parent;
7916}
7917
7918/* Generate the skeleton ancestor tree for the given NODE, then clone
7919 the DIE and add the clone into the tree. */
7920
7921static void
7922generate_skeleton_ancestor_tree (skeleton_chain_node *node)
7923{
7924 if (node->new_die != NULL)
7925 return;
7926
7927 node->new_die = clone_as_declaration (node->old_die);
7928
7929 if (node->parent != NULL)
7930 {
7931 generate_skeleton_ancestor_tree (node->parent);
7932 add_child_die (node->parent->new_die, node->new_die);
7933 }
7934}
7935
7936/* Generate a skeleton tree of DIEs containing any declarations that are
7937 found in the original tree. We traverse the tree looking for declaration
7938 DIEs, and construct the skeleton from the bottom up whenever we find one. */
7939
7940static void
7941generate_skeleton_bottom_up (skeleton_chain_node *parent)
7942{
7943 skeleton_chain_node node;
7944 dw_die_ref c;
7945 dw_die_ref first;
7946 dw_die_ref prev = NULL;
7947 dw_die_ref next = NULL;
7948
7949 node.parent = parent;
7950
7951 first = c = parent->old_die->die_child;
7952 if (c)
7953 next = c->die_sib;
7954 if (c) do {
7955 if (prev == NULL || prev->die_sib == c)
7956 prev = c;
7957 c = next;
7958 next = (c == first ? NULL : c->die_sib);
7959 node.old_die = c;
7960 node.new_die = NULL;
7961 if (is_declaration_die (c))
7962 {
7963 if (is_template_instantiation (c))
7964 {
7965 /* Instantiated templates do not need to be cloned into the
7966 type unit. Just move the DIE and its children back to
7967 the skeleton tree (in the main CU). */
7968 remove_child_with_prev (c, prev);
7969 add_child_die (parent->new_die, c);
7970 c = prev;
7971 }
7972 else if (c->comdat_type_p)
7973 {
7974 /* This is the skeleton of earlier break_out_comdat_types
7975 type. Clone the existing DIE, but keep the children
7976 under the original (which is in the main CU). */
7977 dw_die_ref clone = clone_die (c);
7978
7979 replace_child (c, clone, prev);
7980 generate_skeleton_ancestor_tree (parent);
7981 add_child_die (parent->new_die, c);
7982 c = clone;
7983 continue;
7984 }
7985 else
7986 {
7987 /* Clone the existing DIE, move the original to the skeleton
7988 tree (which is in the main CU), and put the clone, with
7989 all the original's children, where the original came from
7990 (which is about to be moved to the type unit). */
7991 dw_die_ref clone = clone_die (c);
7992 move_all_children (c, clone);
7993
7994 /* If the original has a DW_AT_object_pointer attribute,
7995 it would now point to a child DIE just moved to the
7996 cloned tree, so we need to remove that attribute from
7997 the original. */
7998 remove_AT (c, DW_AT_object_pointer);
7999
8000 replace_child (c, clone, prev);
8001 generate_skeleton_ancestor_tree (parent);
8002 add_child_die (parent->new_die, c);
8003 node.old_die = clone;
8004 node.new_die = c;
8005 c = clone;
8006 }
8007 }
8008 generate_skeleton_bottom_up (&node);
8009 } while (next != NULL);
8010}
8011
8012/* Wrapper function for generate_skeleton_bottom_up. */
8013
8014static dw_die_ref
8015generate_skeleton (dw_die_ref die)
8016{
8017 skeleton_chain_node node;
8018
8019 node.old_die = die;
8020 node.new_die = NULL;
8021 node.parent = NULL;
8022
8023 /* If this type definition is nested inside another type,
8024 and is not an instantiation of a template, always leave
8025 at least a declaration in its place. */
8026 if (die->die_parent != NULL
8027 && is_type_die (die->die_parent)
8028 && !is_template_instantiation (die))
8029 node.new_die = clone_as_declaration (die);
8030
8031 generate_skeleton_bottom_up (&node);
8032 return node.new_die;
8033}
8034
8035/* Remove the CHILD DIE from its parent, possibly replacing it with a cloned
8036 declaration. The original DIE is moved to a new compile unit so that
8037 existing references to it follow it to the new location. If any of the
8038 original DIE's descendants is a declaration, we need to replace the
8039 original DIE with a skeleton tree and move the declarations back into the
8040 skeleton tree. */
8041
8042static dw_die_ref
8043remove_child_or_replace_with_skeleton (dw_die_ref unit, dw_die_ref child,
8044 dw_die_ref prev)
8045{
8046 dw_die_ref skeleton, orig_parent;
8047
8048 /* Copy the declaration context to the type unit DIE. If the returned
8049 ORIG_PARENT is not NULL, the skeleton needs to be added as a child of
8050 that DIE. */
8051 orig_parent = copy_declaration_context (unit, child);
8052
8053 skeleton = generate_skeleton (child);
8054 if (skeleton == NULL)
8055 remove_child_with_prev (child, prev);
8056 else
8057 {
8058 skeleton->comdat_type_p = true;
8059 skeleton->die_id.die_type_node = child->die_id.die_type_node;
8060
8061 /* If the original DIE was a specification, we need to put
8062 the skeleton under the parent DIE of the declaration.
8063 This leaves the original declaration in the tree, but
8064 it will be pruned later since there are no longer any
8065 references to it. */
8066 if (orig_parent != NULL)
8067 {
8068 remove_child_with_prev (child, prev);
8069 add_child_die (orig_parent, skeleton);
8070 }
8071 else
8072 replace_child (child, skeleton, prev);
8073 }
8074
8075 return skeleton;
8076}
8077
8078static void
8079copy_dwarf_procs_ref_in_attrs (dw_die_ref die,
8080 comdat_type_node *type_node,
8081 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs);
8082
8083/* Helper for copy_dwarf_procs_ref_in_dies. Make a copy of the DIE DWARF
8084 procedure, put it under TYPE_NODE and return the copy. Continue looking for
8085 DWARF procedure references in the DW_AT_location attribute. */
8086
8087static dw_die_ref
8088copy_dwarf_procedure (dw_die_ref die,
8089 comdat_type_node *type_node,
8090 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8091{
8092 gcc_assert (die->die_tag == DW_TAG_dwarf_procedure);
8093
8094 /* DWARF procedures are not supposed to have children... */
8095 gcc_assert (die->die_child == NULL);
8096
8097 /* ... and they are supposed to have only one attribute: DW_AT_location. */
8098 gcc_assert (vec_safe_length (die->die_attr) == 1
8099 && ((*die->die_attr)[0].dw_attr == DW_AT_location));
8100
8101 /* Do not copy more than once DWARF procedures. */
8102 bool existed;
8103 dw_die_ref &die_copy = copied_dwarf_procs.get_or_insert (die, &existed);
8104 if (existed)
8105 return die_copy;
8106
8107 die_copy = clone_die (die);
8108 add_child_die (type_node->root_die, die_copy);
8109 copy_dwarf_procs_ref_in_attrs (die_copy, type_node, copied_dwarf_procs);
8110 return die_copy;
8111}
8112
8113/* Helper for copy_dwarf_procs_ref_in_dies. Look for references to DWARF
8114 procedures in DIE's attributes. */
8115
8116static void
8117copy_dwarf_procs_ref_in_attrs (dw_die_ref die,
8118 comdat_type_node *type_node,
8119 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8120{
8121 dw_attr_node *a;
8122 unsigned i;
8123
8124 FOR_EACH_VEC_SAFE_ELT (die->die_attr, i, a)
8125 {
8126 dw_loc_descr_ref loc;
8127
8128 if (a->dw_attr_val.val_class != dw_val_class_loc)
8129 continue;
8130
8131 for (loc = a->dw_attr_val.v.val_loc; loc != NULL; loc = loc->dw_loc_next)
8132 {
8133 switch (loc->dw_loc_opc)
8134 {
8135 case DW_OP_call2:
8136 case DW_OP_call4:
8137 case DW_OP_call_ref:
8138 gcc_assert (loc->dw_loc_oprnd1.val_class
8139 == dw_val_class_die_ref);
8140 loc->dw_loc_oprnd1.v.val_die_ref.die
8141 = copy_dwarf_procedure (loc->dw_loc_oprnd1.v.val_die_ref.die,
8142 type_node,
8143 copied_dwarf_procs);
8144
8145 default:
8146 break;
8147 }
8148 }
8149 }
8150}
8151
8152/* Copy DWARF procedures that are referenced by the DIE tree to TREE_NODE and
8153 rewrite references to point to the copies.
8154
8155 References are looked for in DIE's attributes and recursively in all its
8156 children attributes that are location descriptions. COPIED_DWARF_PROCS is a
8157 mapping from old DWARF procedures to their copy. It is used not to copy
8158 twice the same DWARF procedure under TYPE_NODE. */
8159
8160static void
8161copy_dwarf_procs_ref_in_dies (dw_die_ref die,
8162 comdat_type_node *type_node,
8163 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8164{
8165 dw_die_ref c;
8166
8167 copy_dwarf_procs_ref_in_attrs (die, type_node, copied_dwarf_procs);
8168 FOR_EACH_CHILD (die, c, copy_dwarf_procs_ref_in_dies (c,
8169 type_node,
8170 copied_dwarf_procs));
8171}
8172
8173/* Traverse the DIE and set up additional .debug_types or .debug_info
8174 DW_UT_*type sections for each type worthy of being placed in a COMDAT
8175 section. */
8176
8177static void
8178break_out_comdat_types (dw_die_ref die)
8179{
8180 dw_die_ref c;
8181 dw_die_ref first;
8182 dw_die_ref prev = NULL;
8183 dw_die_ref next = NULL;
8184 dw_die_ref unit = NULL;
8185
8186 first = c = die->die_child;
8187 if (c)
8188 next = c->die_sib;
8189 if (c) do {
8190 if (prev == NULL || prev->die_sib == c)
8191 prev = c;
8192 c = next;
8193 next = (c == first ? NULL : c->die_sib);
8194 if (should_move_die_to_comdat (c))
8195 {
8196 dw_die_ref replacement;
8197 comdat_type_node *type_node;
8198
8199 /* Break out nested types into their own type units. */
8200 break_out_comdat_types (c);
8201
8202 /* Create a new type unit DIE as the root for the new tree, and
8203 add it to the list of comdat types. */
8204 unit = new_die (DW_TAG_type_unit, NULL, NULL);
8205 add_AT_unsigned (unit, DW_AT_language,
8206 get_AT_unsigned (comp_unit_die (), DW_AT_language));
8207 type_node = ggc_cleared_alloc<comdat_type_node> ();
8208 type_node->root_die = unit;
8209 type_node->next = comdat_type_list;
8210 comdat_type_list = type_node;
8211
8212 /* Generate the type signature. */
8213 generate_type_signature (c, type_node);
8214
8215 /* Copy the declaration context, attributes, and children of the
8216 declaration into the new type unit DIE, then remove this DIE
8217 from the main CU (or replace it with a skeleton if necessary). */
8218 replacement = remove_child_or_replace_with_skeleton (unit, c, prev);
8219 type_node->skeleton_die = replacement;
8220
8221 /* Add the DIE to the new compunit. */
8222 add_child_die (unit, c);
8223
8224 /* Types can reference DWARF procedures for type size or data location
8225 expressions. Calls in DWARF expressions cannot target procedures
8226 that are not in the same section. So we must copy DWARF procedures
8227 along with this type and then rewrite references to them. */
8228 hash_map<dw_die_ref, dw_die_ref> copied_dwarf_procs;
8229 copy_dwarf_procs_ref_in_dies (c, type_node, copied_dwarf_procs);
8230
8231 if (replacement != NULL)
8232 c = replacement;
8233 }
8234 else if (c->die_tag == DW_TAG_namespace
8235 || c->die_tag == DW_TAG_class_type
8236 || c->die_tag == DW_TAG_structure_type
8237 || c->die_tag == DW_TAG_union_type)
8238 {
8239 /* Look for nested types that can be broken out. */
8240 break_out_comdat_types (c);
8241 }
8242 } while (next != NULL);
8243}
8244
8245/* Like clone_tree, but copy DW_TAG_subprogram DIEs as declarations.
8246 Enter all the cloned children into the hash table decl_table. */
8247
8248static dw_die_ref
8249clone_tree_partial (dw_die_ref die, decl_hash_type *decl_table)
8250{
8251 dw_die_ref c;
8252 dw_die_ref clone;
8253 struct decl_table_entry *entry;
8254 decl_table_entry **slot;
8255
8256 if (die->die_tag == DW_TAG_subprogram)
8257 clone = clone_as_declaration (die);
8258 else
8259 clone = clone_die (die);
8260
8261 slot = decl_table->find_slot_with_hash (die,
8262 htab_hash_pointer (die), INSERT);
8263
8264 /* Assert that DIE isn't in the hash table yet. If it would be there
8265 before, the ancestors would be necessarily there as well, therefore
8266 clone_tree_partial wouldn't be called. */
8267 gcc_assert (*slot == HTAB_EMPTY_ENTRY);
8268
8269 entry = XCNEW (struct decl_table_entry);
8270 entry->orig = die;
8271 entry->copy = clone;
8272 *slot = entry;
8273
8274 if (die->die_tag != DW_TAG_subprogram)
8275 FOR_EACH_CHILD (die, c,
8276 add_child_die (clone, clone_tree_partial (c, decl_table)));
8277
8278 return clone;
8279}
8280
8281/* Walk the DIE and its children, looking for references to incomplete
8282 or trivial types that are unmarked (i.e., that are not in the current
8283 type_unit). */
8284
8285static void
8286copy_decls_walk (dw_die_ref unit, dw_die_ref die, decl_hash_type *decl_table)
8287{
8288 dw_die_ref c;
8289 dw_attr_node *a;
8290 unsigned ix;
8291
8292 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8293 {
8294 if (AT_class (a) == dw_val_class_die_ref)
8295 {
8296 dw_die_ref targ = AT_ref (a);
8297 decl_table_entry **slot;
8298 struct decl_table_entry *entry;
8299
8300 if (targ->die_mark != 0 || targ->comdat_type_p)
8301 continue;
8302
8303 slot = decl_table->find_slot_with_hash (targ,
8304 htab_hash_pointer (targ),
8305 INSERT);
8306
8307 if (*slot != HTAB_EMPTY_ENTRY)
8308 {
8309 /* TARG has already been copied, so we just need to
8310 modify the reference to point to the copy. */
8311 entry = *slot;
8312 a->dw_attr_val.v.val_die_ref.die = entry->copy;
8313 }
8314 else
8315 {
8316 dw_die_ref parent = unit;
8317 dw_die_ref copy = clone_die (targ);
8318
8319 /* Record in DECL_TABLE that TARG has been copied.
8320 Need to do this now, before the recursive call,
8321 because DECL_TABLE may be expanded and SLOT
8322 would no longer be a valid pointer. */
8323 entry = XCNEW (struct decl_table_entry);
8324 entry->orig = targ;
8325 entry->copy = copy;
8326 *slot = entry;
8327
8328 /* If TARG is not a declaration DIE, we need to copy its
8329 children. */
8330 if (!is_declaration_die (targ))
8331 {
8332 FOR_EACH_CHILD (
8333 targ, c,
8334 add_child_die (copy,
8335 clone_tree_partial (c, decl_table)));
8336 }
8337
8338 /* Make sure the cloned tree is marked as part of the
8339 type unit. */
8340 mark_dies (copy);
8341
8342 /* If TARG has surrounding context, copy its ancestor tree
8343 into the new type unit. */
8344 if (targ->die_parent != NULL
8345 && !is_unit_die (targ->die_parent))
8346 parent = copy_ancestor_tree (unit, targ->die_parent,
8347 decl_table);
8348
8349 add_child_die (parent, copy);
8350 a->dw_attr_val.v.val_die_ref.die = copy;
8351
8352 /* Make sure the newly-copied DIE is walked. If it was
8353 installed in a previously-added context, it won't
8354 get visited otherwise. */
8355 if (parent != unit)
8356 {
8357 /* Find the highest point of the newly-added tree,
8358 mark each node along the way, and walk from there. */
8359 parent->die_mark = 1;
8360 while (parent->die_parent
8361 && parent->die_parent->die_mark == 0)
8362 {
8363 parent = parent->die_parent;
8364 parent->die_mark = 1;
8365 }
8366 copy_decls_walk (unit, parent, decl_table);
8367 }
8368 }
8369 }
8370 }
8371
8372 FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
8373}
8374
8375/* Copy declarations for "unworthy" types into the new comdat section.
8376 Incomplete types, modified types, and certain other types aren't broken
8377 out into comdat sections of their own, so they don't have a signature,
8378 and we need to copy the declaration into the same section so that we
8379 don't have an external reference. */
8380
8381static void
8382copy_decls_for_unworthy_types (dw_die_ref unit)
8383{
8384 mark_dies (unit);
8385 decl_hash_type decl_table (10);
8386 copy_decls_walk (unit, unit, &decl_table);
8387 unmark_dies (unit);
8388}
8389
8390/* Traverse the DIE and add a sibling attribute if it may have the
8391 effect of speeding up access to siblings. To save some space,
8392 avoid generating sibling attributes for DIE's without children. */
8393
8394static void
8395add_sibling_attributes (dw_die_ref die)
8396{
8397 dw_die_ref c;
8398
8399 if (! die->die_child)
8400 return;
8401
8402 if (die->die_parent && die != die->die_parent->die_child)
8403 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
8404
8405 FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
8406}
8407
8408/* Output all location lists for the DIE and its children. */
8409
8410static void
8411output_location_lists (dw_die_ref die)
8412{
8413 dw_die_ref c;
8414 dw_attr_node *a;
8415 unsigned ix;
8416
8417 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8418 if (AT_class (a) == dw_val_class_loc_list)
8419 output_loc_list (AT_loc_list (a));
8420
8421 FOR_EACH_CHILD (die, c, output_location_lists (c));
8422}
8423
8424/* During assign_location_list_indexes and output_loclists_offset the
8425 current index, after it the number of assigned indexes (i.e. how
8426 large the .debug_loclists* offset table should be). */
8427static unsigned int loc_list_idx;
8428
8429/* Output all location list offsets for the DIE and its children. */
8430
8431static void
8432output_loclists_offsets (dw_die_ref die)
8433{
8434 dw_die_ref c;
8435 dw_attr_node *a;
8436 unsigned ix;
8437
8438 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8439 if (AT_class (a) == dw_val_class_loc_list)
8440 {
8441 dw_loc_list_ref l = AT_loc_list (a);
8442 if (l->offset_emitted)
8443 continue;
8444 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l->ll_symbol,
8445 loc_section_label, NULL);
8446 gcc_assert (l->hash == loc_list_idx);
8447 loc_list_idx++;
8448 l->offset_emitted = true;
8449 }
8450
8451 FOR_EACH_CHILD (die, c, output_loclists_offsets (c));
8452}
8453
8454/* Recursively set indexes of location lists. */
8455
8456static void
8457assign_location_list_indexes (dw_die_ref die)
8458{
8459 dw_die_ref c;
8460 dw_attr_node *a;
8461 unsigned ix;
8462
8463 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8464 if (AT_class (a) == dw_val_class_loc_list)
8465 {
8466 dw_loc_list_ref list = AT_loc_list (a);
8467 if (!list->num_assigned)
8468 {
8469 list->num_assigned = true;
8470 list->hash = loc_list_idx++;
8471 }
8472 }
8473
8474 FOR_EACH_CHILD (die, c, assign_location_list_indexes (c));
8475}
8476
8477/* We want to limit the number of external references, because they are
8478 larger than local references: a relocation takes multiple words, and
8479 even a sig8 reference is always eight bytes, whereas a local reference
8480 can be as small as one byte (though DW_FORM_ref is usually 4 in GCC).
8481 So if we encounter multiple external references to the same type DIE, we
8482 make a local typedef stub for it and redirect all references there.
8483
8484 This is the element of the hash table for keeping track of these
8485 references. */
8486
8487struct external_ref
8488{
8489 dw_die_ref type;
8490 dw_die_ref stub;
8491 unsigned n_refs;
8492};
8493
8494/* Hashtable helpers. */
8495
8496struct external_ref_hasher : free_ptr_hash <external_ref>
8497{
8498 static inline hashval_t hash (const external_ref *);
8499 static inline bool equal (const external_ref *, const external_ref *);
8500};
8501
8502inline hashval_t
8503external_ref_hasher::hash (const external_ref *r)
8504{
8505 dw_die_ref die = r->type;
8506 hashval_t h = 0;
8507
8508 /* We can't use the address of the DIE for hashing, because
8509 that will make the order of the stub DIEs non-deterministic. */
8510 if (! die->comdat_type_p)
8511 /* We have a symbol; use it to compute a hash. */
8512 h = htab_hash_string (die->die_id.die_symbol);
8513 else
8514 {
8515 /* We have a type signature; use a subset of the bits as the hash.
8516 The 8-byte signature is at least as large as hashval_t. */
8517 comdat_type_node *type_node = die->die_id.die_type_node;
8518 memcpy (&h, type_node->signature, sizeof (h));
8519 }
8520 return h;
8521}
8522
8523inline bool
8524external_ref_hasher::equal (const external_ref *r1, const external_ref *r2)
8525{
8526 return r1->type == r2->type;
8527}
8528
8529typedef hash_table<external_ref_hasher> external_ref_hash_type;
8530
8531/* Return a pointer to the external_ref for references to DIE. */
8532
8533static struct external_ref *
8534lookup_external_ref (external_ref_hash_type *map, dw_die_ref die)
8535{
8536 struct external_ref ref, *ref_p;
8537 external_ref **slot;
8538
8539 ref.type = die;
8540 slot = map->find_slot (&ref, INSERT);
8541 if (*slot != HTAB_EMPTY_ENTRY)
8542 return *slot;
8543
8544 ref_p = XCNEW (struct external_ref);
8545 ref_p->type = die;
8546 *slot = ref_p;
8547 return ref_p;
8548}
8549
8550/* Subroutine of optimize_external_refs, below.
8551
8552 If we see a type skeleton, record it as our stub. If we see external
8553 references, remember how many we've seen. */
8554
8555static void
8556optimize_external_refs_1 (dw_die_ref die, external_ref_hash_type *map)
8557{
8558 dw_die_ref c;
8559 dw_attr_node *a;
8560 unsigned ix;
8561 struct external_ref *ref_p;
8562
8563 if (is_type_die (die)
8564 && (c = get_AT_ref (die, DW_AT_signature)))
8565 {
8566 /* This is a local skeleton; use it for local references. */
8567 ref_p = lookup_external_ref (map, c);
8568 ref_p->stub = die;
8569 }
8570
8571 /* Scan the DIE references, and remember any that refer to DIEs from
8572 other CUs (i.e. those which are not marked). */
8573 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8574 if (AT_class (a) == dw_val_class_die_ref
8575 && (c = AT_ref (a))->die_mark == 0
8576 && is_type_die (c))
8577 {
8578 ref_p = lookup_external_ref (map, c);
8579 ref_p->n_refs++;
8580 }
8581
8582 FOR_EACH_CHILD (die, c, optimize_external_refs_1 (c, map));
8583}
8584
8585/* htab_traverse callback function for optimize_external_refs, below. SLOT
8586 points to an external_ref, DATA is the CU we're processing. If we don't
8587 already have a local stub, and we have multiple refs, build a stub. */
8588
8589int
8590dwarf2_build_local_stub (external_ref **slot, dw_die_ref data)
8591{
8592 struct external_ref *ref_p = *slot;
8593
8594 if (ref_p->stub == NULL && ref_p->n_refs > 1 && !dwarf_strict)
8595 {
8596 /* We have multiple references to this type, so build a small stub.
8597 Both of these forms are a bit dodgy from the perspective of the
8598 DWARF standard, since technically they should have names. */
8599 dw_die_ref cu = data;
8600 dw_die_ref type = ref_p->type;
8601 dw_die_ref stub = NULL;
8602
8603 if (type->comdat_type_p)
8604 {
8605 /* If we refer to this type via sig8, use AT_signature. */
8606 stub = new_die (type->die_tag, cu, NULL_TREE);
8607 add_AT_die_ref (stub, DW_AT_signature, type);
8608 }
8609 else
8610 {
8611 /* Otherwise, use a typedef with no name. */
8612 stub = new_die (DW_TAG_typedef, cu, NULL_TREE);
8613 add_AT_die_ref (stub, DW_AT_type, type);
8614 }
8615
8616 stub->die_mark++;
8617 ref_p->stub = stub;
8618 }
8619 return 1;
8620}
8621
8622/* DIE is a unit; look through all the DIE references to see if there are
8623 any external references to types, and if so, create local stubs for
8624 them which will be applied in build_abbrev_table. This is useful because
8625 references to local DIEs are smaller. */
8626
8627static external_ref_hash_type *
8628optimize_external_refs (dw_die_ref die)
8629{
8630 external_ref_hash_type *map = new external_ref_hash_type (10);
8631 optimize_external_refs_1 (die, map);
8632 map->traverse <dw_die_ref, dwarf2_build_local_stub> (die);
8633 return map;
8634}
8635
8636/* The following 3 variables are temporaries that are computed only during the
8637 build_abbrev_table call and used and released during the following
8638 optimize_abbrev_table call. */
8639
8640/* First abbrev_id that can be optimized based on usage. */
8641static unsigned int abbrev_opt_start;
8642
8643/* Maximum abbrev_id of a base type plus one (we can't optimize DIEs with
8644 abbrev_id smaller than this, because they must be already sized
8645 during build_abbrev_table). */
8646static unsigned int abbrev_opt_base_type_end;
8647
8648/* Vector of usage counts during build_abbrev_table. Indexed by
8649 abbrev_id - abbrev_opt_start. */
8650static vec<unsigned int> abbrev_usage_count;
8651
8652/* Vector of all DIEs added with die_abbrev >= abbrev_opt_start. */
8653static vec<dw_die_ref> sorted_abbrev_dies;
8654
8655/* The format of each DIE (and its attribute value pairs) is encoded in an
8656 abbreviation table. This routine builds the abbreviation table and assigns
8657 a unique abbreviation id for each abbreviation entry. The children of each
8658 die are visited recursively. */
8659
8660static void
8661build_abbrev_table (dw_die_ref die, external_ref_hash_type *extern_map)
8662{
8663 unsigned int abbrev_id = 0;
8664 dw_die_ref c;
8665 dw_attr_node *a;
8666 unsigned ix;
8667 dw_die_ref abbrev;
8668
8669 /* Scan the DIE references, and replace any that refer to
8670 DIEs from other CUs (i.e. those which are not marked) with
8671 the local stubs we built in optimize_external_refs. */
8672 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8673 if (AT_class (a) == dw_val_class_die_ref
8674 && (c = AT_ref (a))->die_mark == 0)
8675 {
8676 struct external_ref *ref_p;
8677 gcc_assert (AT_ref (a)->comdat_type_p || AT_ref (a)->die_id.die_symbol);
8678
8679 ref_p = lookup_external_ref (extern_map, c);
8680 if (ref_p->stub && ref_p->stub != die)
8681 change_AT_die_ref (a, ref_p->stub);
8682 else
8683 /* We aren't changing this reference, so mark it external. */
8684 set_AT_ref_external (a, 1);
8685 }
8686
8687 FOR_EACH_VEC_SAFE_ELT (abbrev_die_table, abbrev_id, abbrev)
8688 {
8689 dw_attr_node *die_a, *abbrev_a;
8690 unsigned ix;
8691 bool ok = true;
8692
8693 if (abbrev_id == 0)
8694 continue;
8695 if (abbrev->die_tag != die->die_tag)
8696 continue;
8697 if ((abbrev->die_child != NULL) != (die->die_child != NULL))
8698 continue;
8699
8700 if (vec_safe_length (abbrev->die_attr) != vec_safe_length (die->die_attr))
8701 continue;
8702
8703 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, die_a)
8704 {
8705 abbrev_a = &(*abbrev->die_attr)[ix];
8706 if ((abbrev_a->dw_attr != die_a->dw_attr)
8707 || (value_format (abbrev_a) != value_format (die_a)))
8708 {
8709 ok = false;
8710 break;
8711 }
8712 }
8713 if (ok)
8714 break;
8715 }
8716
8717 if (abbrev_id >= vec_safe_length (abbrev_die_table))
8718 {
8719 vec_safe_push (abbrev_die_table, die);
8720 if (abbrev_opt_start)
8721 abbrev_usage_count.safe_push (0);
8722 }
8723 if (abbrev_opt_start && abbrev_id >= abbrev_opt_start)
8724 {
8725 abbrev_usage_count[abbrev_id - abbrev_opt_start]++;
8726 sorted_abbrev_dies.safe_push (die);
8727 }
8728
8729 die->die_abbrev = abbrev_id;
8730 FOR_EACH_CHILD (die, c, build_abbrev_table (c, extern_map));
8731}
8732
8733/* Callback function for sorted_abbrev_dies vector sorting. We sort
8734 by die_abbrev's usage count, from the most commonly used
8735 abbreviation to the least. */
8736
8737static int
8738die_abbrev_cmp (const void *p1, const void *p2)
8739{
8740 dw_die_ref die1 = *(const dw_die_ref *) p1;
8741 dw_die_ref die2 = *(const dw_die_ref *) p2;
8742
8743 gcc_checking_assert (die1->die_abbrev >= abbrev_opt_start);
8744 gcc_checking_assert (die2->die_abbrev >= abbrev_opt_start);
8745
8746 if (die1->die_abbrev >= abbrev_opt_base_type_end
8747 && die2->die_abbrev >= abbrev_opt_base_type_end)
8748 {
8749 if (abbrev_usage_count[die1->die_abbrev - abbrev_opt_start]
8750 > abbrev_usage_count[die2->die_abbrev - abbrev_opt_start])
8751 return -1;
8752 if (abbrev_usage_count[die1->die_abbrev - abbrev_opt_start]
8753 < abbrev_usage_count[die2->die_abbrev - abbrev_opt_start])
8754 return 1;
8755 }
8756
8757 /* Stabilize the sort. */
8758 if (die1->die_abbrev < die2->die_abbrev)
8759 return -1;
8760 if (die1->die_abbrev > die2->die_abbrev)
8761 return 1;
8762
8763 return 0;
8764}
8765
8766/* Convert dw_val_class_const and dw_val_class_unsigned_const class attributes
8767 of DIEs in between sorted_abbrev_dies[first_id] and abbrev_dies[end_id - 1]
8768 into dw_val_class_const_implicit or
8769 dw_val_class_unsigned_const_implicit. */
8770
8771static void
8772optimize_implicit_const (unsigned int first_id, unsigned int end,
8773 vec<bool> &implicit_consts)
8774{
8775 /* It never makes sense if there is just one DIE using the abbreviation. */
8776 if (end < first_id + 2)
8777 return;
8778
8779 dw_attr_node *a;
8780 unsigned ix, i;
8781 dw_die_ref die = sorted_abbrev_dies[first_id];
8782 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8783 if (implicit_consts[ix])
8784 {
8785 enum dw_val_class new_class = dw_val_class_none;
8786 switch (AT_class (a))
8787 {
8788 case dw_val_class_unsigned_const:
8789 if ((HOST_WIDE_INT) AT_unsigned (a) < 0)
8790 continue;
8791
8792 /* The .debug_abbrev section will grow by
8793 size_of_sleb128 (AT_unsigned (a)) and we avoid the constants
8794 in all the DIEs using that abbreviation. */
8795 if (constant_size (AT_unsigned (a)) * (end - first_id)
8796 <= (unsigned) size_of_sleb128 (AT_unsigned (a)))
8797 continue;
8798
8799 new_class = dw_val_class_unsigned_const_implicit;
8800 break;
8801
8802 case dw_val_class_const:
8803 new_class = dw_val_class_const_implicit;
8804 break;
8805
8806 case dw_val_class_file:
8807 new_class = dw_val_class_file_implicit;
8808 break;
8809
8810 default:
8811 continue;
8812 }
8813 for (i = first_id; i < end; i++)
8814 (*sorted_abbrev_dies[i]->die_attr)[ix].dw_attr_val.val_class
8815 = new_class;
8816 }
8817}
8818
8819/* Attempt to optimize abbreviation table from abbrev_opt_start
8820 abbreviation above. */
8821
8822static void
8823optimize_abbrev_table (void)
8824{
8825 if (abbrev_opt_start
8826 && vec_safe_length (abbrev_die_table) > abbrev_opt_start
8827 && (dwarf_version >= 5 || vec_safe_length (abbrev_die_table) > 127))
8828 {
8829 auto_vec<bool, 32> implicit_consts;
8830 sorted_abbrev_dies.qsort (die_abbrev_cmp);
8831
8832 unsigned int abbrev_id = abbrev_opt_start - 1;
8833 unsigned int first_id = ~0U;
8834 unsigned int last_abbrev_id = 0;
8835 unsigned int i;
8836 dw_die_ref die;
8837 if (abbrev_opt_base_type_end > abbrev_opt_start)
8838 abbrev_id = abbrev_opt_base_type_end - 1;
8839 /* Reassign abbreviation ids from abbrev_opt_start above, so that
8840 most commonly used abbreviations come first. */
8841 FOR_EACH_VEC_ELT (sorted_abbrev_dies, i, die)
8842 {
8843 dw_attr_node *a;
8844 unsigned ix;
8845
8846 /* If calc_base_type_die_sizes has been called, the CU and
8847 base types after it can't be optimized, because we've already
8848 calculated their DIE offsets. We've sorted them first. */
8849 if (die->die_abbrev < abbrev_opt_base_type_end)
8850 continue;
8851 if (die->die_abbrev != last_abbrev_id)
8852 {
8853 last_abbrev_id = die->die_abbrev;
8854 if (dwarf_version >= 5 && first_id != ~0U)
8855 optimize_implicit_const (first_id, i, implicit_consts);
8856 abbrev_id++;
8857 (*abbrev_die_table)[abbrev_id] = die;
8858 if (dwarf_version >= 5)
8859 {
8860 first_id = i;
8861 implicit_consts.truncate (0);
8862
8863 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8864 switch (AT_class (a))
8865 {
8866 case dw_val_class_const:
8867 case dw_val_class_unsigned_const:
8868 case dw_val_class_file:
8869 implicit_consts.safe_push (true);
8870 break;
8871 default:
8872 implicit_consts.safe_push (false);
8873 break;
8874 }
8875 }
8876 }
8877 else if (dwarf_version >= 5)
8878 {
8879 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8880 if (!implicit_consts[ix])
8881 continue;
8882 else
8883 {
8884 dw_attr_node *other_a
8885 = &(*(*abbrev_die_table)[abbrev_id]->die_attr)[ix];
8886 if (!dw_val_equal_p (&a->dw_attr_val,
8887 &other_a->dw_attr_val))
8888 implicit_consts[ix] = false;
8889 }
8890 }
8891 die->die_abbrev = abbrev_id;
8892 }
8893 gcc_assert (abbrev_id == vec_safe_length (abbrev_die_table) - 1);
8894 if (dwarf_version >= 5 && first_id != ~0U)
8895 optimize_implicit_const (first_id, i, implicit_consts);
8896 }
8897
8898 abbrev_opt_start = 0;
8899 abbrev_opt_base_type_end = 0;
8900 abbrev_usage_count.release ();
8901 sorted_abbrev_dies.release ();
8902}
8903
8904/* Return the power-of-two number of bytes necessary to represent VALUE. */
8905
8906static int
8907constant_size (unsigned HOST_WIDE_INT value)
8908{
8909 int log;
8910
8911 if (value == 0)
8912 log = 0;
8913 else
8914 log = floor_log2 (value);
8915
8916 log = log / 8;
8917 log = 1 << (floor_log2 (log) + 1);
8918
8919 return log;
8920}
8921
8922/* Return the size of a DIE as it is represented in the
8923 .debug_info section. */
8924
8925static unsigned long
8926size_of_die (dw_die_ref die)
8927{
8928 unsigned long size = 0;
8929 dw_attr_node *a;
8930 unsigned ix;
8931 enum dwarf_form form;
8932
8933 size += size_of_uleb128 (die->die_abbrev);
8934 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8935 {
8936 switch (AT_class (a))
8937 {
8938 case dw_val_class_addr:
8939 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8940 {
8941 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
8942 size += size_of_uleb128 (AT_index (a));
8943 }
8944 else
8945 size += DWARF2_ADDR_SIZE;
8946 break;
8947 case dw_val_class_offset:
8948 size += DWARF_OFFSET_SIZE;
8949 break;
8950 case dw_val_class_loc:
8951 {
8952 unsigned long lsize = size_of_locs (AT_loc (a));
8953
8954 /* Block length. */
8955 if (dwarf_version >= 4)
8956 size += size_of_uleb128 (lsize);
8957 else
8958 size += constant_size (lsize);
8959 size += lsize;
8960 }
8961 break;
8962 case dw_val_class_loc_list:
8963 if (dwarf_split_debug_info && dwarf_version >= 5)
8964 {
8965 gcc_assert (AT_loc_list (a)->num_assigned);
8966 size += size_of_uleb128 (AT_loc_list (a)->hash);
8967 }
8968 else
8969 size += DWARF_OFFSET_SIZE;
8970 break;
8971 case dw_val_class_range_list:
8972 if (value_format (a) == DW_FORM_rnglistx)
8973 {
8974 gcc_assert (rnglist_idx);
8975 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
8976 size += size_of_uleb128 (r->idx);
8977 }
8978 else
8979 size += DWARF_OFFSET_SIZE;
8980 break;
8981 case dw_val_class_const:
8982 size += size_of_sleb128 (AT_int (a));
8983 break;
8984 case dw_val_class_unsigned_const:
8985 {
8986 int csize = constant_size (AT_unsigned (a));
8987 if (dwarf_version == 3
8988 && a->dw_attr == DW_AT_data_member_location
8989 && csize >= 4)
8990 size += size_of_uleb128 (AT_unsigned (a));
8991 else
8992 size += csize;
8993 }
8994 break;
8995 case dw_val_class_const_implicit:
8996 case dw_val_class_unsigned_const_implicit:
8997 case dw_val_class_file_implicit:
8998 /* These occupy no size in the DIE, just an extra sleb128 in
8999 .debug_abbrev. */
9000 break;
9001 case dw_val_class_const_double:
9002 size += HOST_BITS_PER_DOUBLE_INT / HOST_BITS_PER_CHAR;
9003 if (HOST_BITS_PER_WIDE_INT >= DWARF_LARGEST_DATA_FORM_BITS)
9004 size++; /* block */
9005 break;
9006 case dw_val_class_wide_int:
9007 size += (get_full_len (*a->dw_attr_val.v.val_wide)
9008 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
9009 if (get_full_len (*a->dw_attr_val.v.val_wide)
9010 * HOST_BITS_PER_WIDE_INT > DWARF_LARGEST_DATA_FORM_BITS)
9011 size++; /* block */
9012 break;
9013 case dw_val_class_vec:
9014 size += constant_size (a->dw_attr_val.v.val_vec.length
9015 * a->dw_attr_val.v.val_vec.elt_size)
9016 + a->dw_attr_val.v.val_vec.length
9017 * a->dw_attr_val.v.val_vec.elt_size; /* block */
9018 break;
9019 case dw_val_class_flag:
9020 if (dwarf_version >= 4)
9021 /* Currently all add_AT_flag calls pass in 1 as last argument,
9022 so DW_FORM_flag_present can be used. If that ever changes,
9023 we'll need to use DW_FORM_flag and have some optimization
9024 in build_abbrev_table that will change those to
9025 DW_FORM_flag_present if it is set to 1 in all DIEs using
9026 the same abbrev entry. */
9027 gcc_assert (a->dw_attr_val.v.val_flag == 1);
9028 else
9029 size += 1;
9030 break;
9031 case dw_val_class_die_ref:
9032 if (AT_ref_external (a))
9033 {
9034 /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
9035 we use DW_FORM_ref_addr. In DWARF2, DW_FORM_ref_addr
9036 is sized by target address length, whereas in DWARF3
9037 it's always sized as an offset. */
9038 if (use_debug_types)
9039 size += DWARF_TYPE_SIGNATURE_SIZE;
9040 else if (dwarf_version == 2)
9041 size += DWARF2_ADDR_SIZE;
9042 else
9043 size += DWARF_OFFSET_SIZE;
9044 }
9045 else
9046 size += DWARF_OFFSET_SIZE;
9047 break;
9048 case dw_val_class_fde_ref:
9049 size += DWARF_OFFSET_SIZE;
9050 break;
9051 case dw_val_class_lbl_id:
9052 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
9053 {
9054 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
9055 size += size_of_uleb128 (AT_index (a));
9056 }
9057 else
9058 size += DWARF2_ADDR_SIZE;
9059 break;
9060 case dw_val_class_lineptr:
9061 case dw_val_class_macptr:
9062 case dw_val_class_loclistsptr:
9063 size += DWARF_OFFSET_SIZE;
9064 break;
9065 case dw_val_class_str:
9066 form = AT_string_form (a);
9067 if (form == DW_FORM_strp || form == DW_FORM_line_strp)
9068 size += DWARF_OFFSET_SIZE;
9069 else if (form == DW_FORM_GNU_str_index)
9070 size += size_of_uleb128 (AT_index (a));
9071 else
9072 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
9073 break;
9074 case dw_val_class_file:
9075 size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
9076 break;
9077 case dw_val_class_data8:
9078 size += 8;
9079 break;
9080 case dw_val_class_vms_delta:
9081 size += DWARF_OFFSET_SIZE;
9082 break;
9083 case dw_val_class_high_pc:
9084 size += DWARF2_ADDR_SIZE;
9085 break;
9086 case dw_val_class_discr_value:
9087 size += size_of_discr_value (&a->dw_attr_val.v.val_discr_value);
9088 break;
9089 case dw_val_class_discr_list:
9090 {
9091 unsigned block_size = size_of_discr_list (AT_discr_list (a));
9092
9093 /* This is a block, so we have the block length and then its
9094 data. */
9095 size += constant_size (block_size) + block_size;
9096 }
9097 break;
9098 default:
9099 gcc_unreachable ();
9100 }
9101 }
9102
9103 return size;
9104}
9105
9106/* Size the debugging information associated with a given DIE. Visits the
9107 DIE's children recursively. Updates the global variable next_die_offset, on
9108 each time through. Uses the current value of next_die_offset to update the
9109 die_offset field in each DIE. */
9110
9111static void
9112calc_die_sizes (dw_die_ref die)
9113{
9114 dw_die_ref c;
9115
9116 gcc_assert (die->die_offset == 0
9117 || (unsigned long int) die->die_offset == next_die_offset);
9118 die->die_offset = next_die_offset;
9119 next_die_offset += size_of_die (die);
9120
9121 FOR_EACH_CHILD (die, c, calc_die_sizes (c));
9122
9123 if (die->die_child != NULL)
9124 /* Count the null byte used to terminate sibling lists. */
9125 next_die_offset += 1;
9126}
9127
9128/* Size just the base type children at the start of the CU.
9129 This is needed because build_abbrev needs to size locs
9130 and sizing of type based stack ops needs to know die_offset
9131 values for the base types. */
9132
9133static void
9134calc_base_type_die_sizes (void)
9135{
9136 unsigned long die_offset = (dwarf_split_debug_info
9137 ? DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE
9138 : DWARF_COMPILE_UNIT_HEADER_SIZE);
9139 unsigned int i;
9140 dw_die_ref base_type;
9141#if ENABLE_ASSERT_CHECKING
9142 dw_die_ref prev = comp_unit_die ()->die_child;
9143#endif
9144
9145 die_offset += size_of_die (comp_unit_die ());
9146 for (i = 0; base_types.iterate (i, &base_type); i++)
9147 {
9148#if ENABLE_ASSERT_CHECKING
9149 gcc_assert (base_type->die_offset == 0
9150 && prev->die_sib == base_type
9151 && base_type->die_child == NULL
9152 && base_type->die_abbrev);
9153 prev = base_type;
9154#endif
9155 if (abbrev_opt_start
9156 && base_type->die_abbrev >= abbrev_opt_base_type_end)
9157 abbrev_opt_base_type_end = base_type->die_abbrev + 1;
9158 base_type->die_offset = die_offset;
9159 die_offset += size_of_die (base_type);
9160 }
9161}
9162
9163/* Set the marks for a die and its children. We do this so
9164 that we know whether or not a reference needs to use FORM_ref_addr; only
9165 DIEs in the same CU will be marked. We used to clear out the offset
9166 and use that as the flag, but ran into ordering problems. */
9167
9168static void
9169mark_dies (dw_die_ref die)
9170{
9171 dw_die_ref c;
9172
9173 gcc_assert (!die->die_mark);
9174
9175 die->die_mark = 1;
9176 FOR_EACH_CHILD (die, c, mark_dies (c));
9177}
9178
9179/* Clear the marks for a die and its children. */
9180
9181static void
9182unmark_dies (dw_die_ref die)
9183{
9184 dw_die_ref c;
9185
9186 if (! use_debug_types)
9187 gcc_assert (die->die_mark);
9188
9189 die->die_mark = 0;
9190 FOR_EACH_CHILD (die, c, unmark_dies (c));
9191}
9192
9193/* Clear the marks for a die, its children and referred dies. */
9194
9195static void
9196unmark_all_dies (dw_die_ref die)
9197{
9198 dw_die_ref c;
9199 dw_attr_node *a;
9200 unsigned ix;
9201
9202 if (!die->die_mark)
9203 return;
9204 die->die_mark = 0;
9205
9206 FOR_EACH_CHILD (die, c, unmark_all_dies (c));
9207
9208 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9209 if (AT_class (a) == dw_val_class_die_ref)
9210 unmark_all_dies (AT_ref (a));
9211}
9212
9213/* Calculate if the entry should appear in the final output file. It may be
9214 from a pruned a type. */
9215
9216static bool
9217include_pubname_in_output (vec<pubname_entry, va_gc> *table, pubname_entry *p)
9218{
9219 /* By limiting gnu pubnames to definitions only, gold can generate a
9220 gdb index without entries for declarations, which don't include
9221 enough information to be useful. */
9222 if (debug_generate_pub_sections == 2 && is_declaration_die (p->die))
9223 return false;
9224
9225 if (table == pubname_table)
9226 {
9227 /* Enumerator names are part of the pubname table, but the
9228 parent DW_TAG_enumeration_type die may have been pruned.
9229 Don't output them if that is the case. */
9230 if (p->die->die_tag == DW_TAG_enumerator &&
9231 (p->die->die_parent == NULL
9232 || !p->die->die_parent->die_perennial_p))
9233 return false;
9234
9235 /* Everything else in the pubname table is included. */
9236 return true;
9237 }
9238
9239 /* The pubtypes table shouldn't include types that have been
9240 pruned. */
9241 return (p->die->die_offset != 0
9242 || !flag_eliminate_unused_debug_types);
9243}
9244
9245/* Return the size of the .debug_pubnames or .debug_pubtypes table
9246 generated for the compilation unit. */
9247
9248static unsigned long
9249size_of_pubnames (vec<pubname_entry, va_gc> *names)
9250{
9251 unsigned long size;
9252 unsigned i;
9253 pubname_entry *p;
9254 int space_for_flags = (debug_generate_pub_sections == 2) ? 1 : 0;
9255
9256 size = DWARF_PUBNAMES_HEADER_SIZE;
9257 FOR_EACH_VEC_ELT (*names, i, p)
9258 if (include_pubname_in_output (names, p))
9259 size += strlen (p->name) + DWARF_OFFSET_SIZE + 1 + space_for_flags;
9260
9261 size += DWARF_OFFSET_SIZE;
9262 return size;
9263}
9264
9265/* Return the size of the information in the .debug_aranges section. */
9266
9267static unsigned long
9268size_of_aranges (void)
9269{
9270 unsigned long size;
9271
9272 size = DWARF_ARANGES_HEADER_SIZE;
9273
9274 /* Count the address/length pair for this compilation unit. */
9275 if (text_section_used)
9276 size += 2 * DWARF2_ADDR_SIZE;
9277 if (cold_text_section_used)
9278 size += 2 * DWARF2_ADDR_SIZE;
9279 if (have_multiple_function_sections)
9280 {
9281 unsigned fde_idx;
9282 dw_fde_ref fde;
9283
9284 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
9285 {
9286 if (DECL_IGNORED_P (fde->decl))
9287 continue;
9288 if (!fde->in_std_section)
9289 size += 2 * DWARF2_ADDR_SIZE;
9290 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
9291 size += 2 * DWARF2_ADDR_SIZE;
9292 }
9293 }
9294
9295 /* Count the two zero words used to terminated the address range table. */
9296 size += 2 * DWARF2_ADDR_SIZE;
9297 return size;
9298}
9299
9300/* Select the encoding of an attribute value. */
9301
9302static enum dwarf_form
9303value_format (dw_attr_node *a)
9304{
9305 switch (AT_class (a))
9306 {
9307 case dw_val_class_addr:
9308 /* Only very few attributes allow DW_FORM_addr. */
9309 switch (a->dw_attr)
9310 {
9311 case DW_AT_low_pc:
9312 case DW_AT_high_pc:
9313 case DW_AT_entry_pc:
9314 case DW_AT_trampoline:
9315 return (AT_index (a) == NOT_INDEXED
9316 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
9317 default:
9318 break;
9319 }
9320 switch (DWARF2_ADDR_SIZE)
9321 {
9322 case 1:
9323 return DW_FORM_data1;
9324 case 2:
9325 return DW_FORM_data2;
9326 case 4:
9327 return DW_FORM_data4;
9328 case 8:
9329 return DW_FORM_data8;
9330 default:
9331 gcc_unreachable ();
9332 }
9333 case dw_val_class_loc_list:
9334 if (dwarf_split_debug_info
9335 && dwarf_version >= 5
9336 && AT_loc_list (a)->num_assigned)
9337 return DW_FORM_loclistx;
9338 /* FALLTHRU */
9339 case dw_val_class_range_list:
9340 /* For range lists in DWARF 5, use DW_FORM_rnglistx from .debug_info.dwo
9341 but in .debug_info use DW_FORM_sec_offset, which is shorter if we
9342 care about sizes of .debug* sections in shared libraries and
9343 executables and don't take into account relocations that affect just
9344 relocatable objects - for DW_FORM_rnglistx we'd have to emit offset
9345 table in the .debug_rnglists section. */
9346 if (dwarf_split_debug_info
9347 && dwarf_version >= 5
9348 && AT_class (a) == dw_val_class_range_list
9349 && rnglist_idx
9350 && a->dw_attr_val.val_entry != RELOCATED_OFFSET)
9351 return DW_FORM_rnglistx;
9352 if (dwarf_version >= 4)
9353 return DW_FORM_sec_offset;
9354 /* FALLTHRU */
9355 case dw_val_class_vms_delta:
9356 case dw_val_class_offset:
9357 switch (DWARF_OFFSET_SIZE)
9358 {
9359 case 4:
9360 return DW_FORM_data4;
9361 case 8:
9362 return DW_FORM_data8;
9363 default:
9364 gcc_unreachable ();
9365 }
9366 case dw_val_class_loc:
9367 if (dwarf_version >= 4)
9368 return DW_FORM_exprloc;
9369 switch (constant_size (size_of_locs (AT_loc (a))))
9370 {
9371 case 1:
9372 return DW_FORM_block1;
9373 case 2:
9374 return DW_FORM_block2;
9375 case 4:
9376 return DW_FORM_block4;
9377 default:
9378 gcc_unreachable ();
9379 }
9380 case dw_val_class_const:
9381 return DW_FORM_sdata;
9382 case dw_val_class_unsigned_const:
9383 switch (constant_size (AT_unsigned (a)))
9384 {
9385 case 1:
9386 return DW_FORM_data1;
9387 case 2:
9388 return DW_FORM_data2;
9389 case 4:
9390 /* In DWARF3 DW_AT_data_member_location with
9391 DW_FORM_data4 or DW_FORM_data8 is a loclistptr, not
9392 constant, so we need to use DW_FORM_udata if we need
9393 a large constant. */
9394 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
9395 return DW_FORM_udata;
9396 return DW_FORM_data4;
9397 case 8:
9398 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
9399 return DW_FORM_udata;
9400 return DW_FORM_data8;
9401 default:
9402 gcc_unreachable ();
9403 }
9404 case dw_val_class_const_implicit:
9405 case dw_val_class_unsigned_const_implicit:
9406 case dw_val_class_file_implicit:
9407 return DW_FORM_implicit_const;
9408 case dw_val_class_const_double:
9409 switch (HOST_BITS_PER_WIDE_INT)
9410 {
9411 case 8:
9412 return DW_FORM_data2;
9413 case 16:
9414 return DW_FORM_data4;
9415 case 32:
9416 return DW_FORM_data8;
9417 case 64:
9418 if (dwarf_version >= 5)
9419 return DW_FORM_data16;
9420 /* FALLTHRU */
9421 default:
9422 return DW_FORM_block1;
9423 }
9424 case dw_val_class_wide_int:
9425 switch (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT)
9426 {
9427 case 8:
9428 return DW_FORM_data1;
9429 case 16:
9430 return DW_FORM_data2;
9431 case 32:
9432 return DW_FORM_data4;
9433 case 64:
9434 return DW_FORM_data8;
9435 case 128:
9436 if (dwarf_version >= 5)
9437 return DW_FORM_data16;
9438 /* FALLTHRU */
9439 default:
9440 return DW_FORM_block1;
9441 }
9442 case dw_val_class_vec:
9443 switch (constant_size (a->dw_attr_val.v.val_vec.length
9444 * a->dw_attr_val.v.val_vec.elt_size))
9445 {
9446 case 1:
9447 return DW_FORM_block1;
9448 case 2:
9449 return DW_FORM_block2;
9450 case 4:
9451 return DW_FORM_block4;
9452 default:
9453 gcc_unreachable ();
9454 }
9455 case dw_val_class_flag:
9456 if (dwarf_version >= 4)
9457 {
9458 /* Currently all add_AT_flag calls pass in 1 as last argument,
9459 so DW_FORM_flag_present can be used. If that ever changes,
9460 we'll need to use DW_FORM_flag and have some optimization
9461 in build_abbrev_table that will change those to
9462 DW_FORM_flag_present if it is set to 1 in all DIEs using
9463 the same abbrev entry. */
9464 gcc_assert (a->dw_attr_val.v.val_flag == 1);
9465 return DW_FORM_flag_present;
9466 }
9467 return DW_FORM_flag;
9468 case dw_val_class_die_ref:
9469 if (AT_ref_external (a))
9470 return use_debug_types ? DW_FORM_ref_sig8 : DW_FORM_ref_addr;
9471 else
9472 return DW_FORM_ref;
9473 case dw_val_class_fde_ref:
9474 return DW_FORM_data;
9475 case dw_val_class_lbl_id:
9476 return (AT_index (a) == NOT_INDEXED
9477 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
9478 case dw_val_class_lineptr:
9479 case dw_val_class_macptr:
9480 case dw_val_class_loclistsptr:
9481 return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
9482 case dw_val_class_str:
9483 return AT_string_form (a);
9484 case dw_val_class_file:
9485 switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
9486 {
9487 case 1:
9488 return DW_FORM_data1;
9489 case 2:
9490 return DW_FORM_data2;
9491 case 4:
9492 return DW_FORM_data4;
9493 default:
9494 gcc_unreachable ();
9495 }
9496
9497 case dw_val_class_data8:
9498 return DW_FORM_data8;
9499
9500 case dw_val_class_high_pc:
9501 switch (DWARF2_ADDR_SIZE)
9502 {
9503 case 1:
9504 return DW_FORM_data1;
9505 case 2:
9506 return DW_FORM_data2;
9507 case 4:
9508 return DW_FORM_data4;
9509 case 8:
9510 return DW_FORM_data8;
9511 default:
9512 gcc_unreachable ();
9513 }
9514
9515 case dw_val_class_discr_value:
9516 return (a->dw_attr_val.v.val_discr_value.pos
9517 ? DW_FORM_udata
9518 : DW_FORM_sdata);
9519 case dw_val_class_discr_list:
9520 switch (constant_size (size_of_discr_list (AT_discr_list (a))))
9521 {
9522 case 1:
9523 return DW_FORM_block1;
9524 case 2:
9525 return DW_FORM_block2;
9526 case 4:
9527 return DW_FORM_block4;
9528 default:
9529 gcc_unreachable ();
9530 }
9531
9532 default:
9533 gcc_unreachable ();
9534 }
9535}
9536
9537/* Output the encoding of an attribute value. */
9538
9539static void
9540output_value_format (dw_attr_node *a)
9541{
9542 enum dwarf_form form = value_format (a);
9543
9544 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
9545}
9546
9547/* Given a die and id, produce the appropriate abbreviations. */
9548
9549static void
9550output_die_abbrevs (unsigned long abbrev_id, dw_die_ref abbrev)
9551{
9552 unsigned ix;
9553 dw_attr_node *a_attr;
9554
9555 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
9556 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
9557 dwarf_tag_name (abbrev->die_tag));
9558
9559 if (abbrev->die_child != NULL)
9560 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
9561 else
9562 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
9563
9564 for (ix = 0; vec_safe_iterate (abbrev->die_attr, ix, &a_attr); ix++)
9565 {
9566 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
9567 dwarf_attr_name (a_attr->dw_attr));
9568 output_value_format (a_attr);
9569 if (value_format (a_attr) == DW_FORM_implicit_const)
9570 {
9571 if (AT_class (a_attr) == dw_val_class_file_implicit)
9572 {
9573 int f = maybe_emit_file (a_attr->dw_attr_val.v.val_file);
9574 const char *filename = a_attr->dw_attr_val.v.val_file->filename;
9575 dw2_asm_output_data_sleb128 (f, "(%s)", filename);
9576 }
9577 else
9578 dw2_asm_output_data_sleb128 (a_attr->dw_attr_val.v.val_int, NULL);
9579 }
9580 }
9581
9582 dw2_asm_output_data (1, 0, NULL);
9583 dw2_asm_output_data (1, 0, NULL);
9584}
9585
9586
9587/* Output the .debug_abbrev section which defines the DIE abbreviation
9588 table. */
9589
9590static void
9591output_abbrev_section (void)
9592{
9593 unsigned int abbrev_id;
9594 dw_die_ref abbrev;
9595
9596 FOR_EACH_VEC_SAFE_ELT (abbrev_die_table, abbrev_id, abbrev)
9597 if (abbrev_id != 0)
9598 output_die_abbrevs (abbrev_id, abbrev);
9599
9600 /* Terminate the table. */
9601 dw2_asm_output_data (1, 0, NULL);
9602}
9603
9604/* Output a symbol we can use to refer to this DIE from another CU. */
9605
9606static inline void
9607output_die_symbol (dw_die_ref die)
9608{
9609 const char *sym = die->die_id.die_symbol;
9610
9611 gcc_assert (!die->comdat_type_p);
9612
9613 if (sym == 0)
9614 return;
9615
9616 if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
9617 /* We make these global, not weak; if the target doesn't support
9618 .linkonce, it doesn't support combining the sections, so debugging
9619 will break. */
9620 targetm.asm_out.globalize_label (asm_out_file, sym);
9621
9622 ASM_OUTPUT_LABEL (asm_out_file, sym);
9623}
9624
9625/* Return a new location list, given the begin and end range, and the
9626 expression. */
9627
9628static inline dw_loc_list_ref
9629new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
9630 const char *section)
9631{
9632 dw_loc_list_ref retlist = ggc_cleared_alloc<dw_loc_list_node> ();
9633
9634 retlist->begin = begin;
9635 retlist->begin_entry = NULL;
9636 retlist->end = end;
9637 retlist->expr = expr;
9638 retlist->section = section;
9639
9640 return retlist;
9641}
9642
9643/* Generate a new internal symbol for this location list node, if it
9644 hasn't got one yet. */
9645
9646static inline void
9647gen_llsym (dw_loc_list_ref list)
9648{
9649 gcc_assert (!list->ll_symbol);
9650 list->ll_symbol = gen_internal_sym ("LLST");
9651}
9652
9653/* Output the location list given to us. */
9654
9655static void
9656output_loc_list (dw_loc_list_ref list_head)
9657{
9658 if (list_head->emitted)
9659 return;
9660 list_head->emitted = true;
9661
9662 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
9663
9664 dw_loc_list_ref curr = list_head;
9665 const char *last_section = NULL;
9666 const char *base_label = NULL;
9667
9668 /* Walk the location list, and output each range + expression. */
9669 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
9670 {
9671 unsigned long size;
9672 /* Don't output an entry that starts and ends at the same address. */
9673 if (strcmp (curr->begin, curr->end) == 0 && !curr->force)
9674 continue;
9675 size = size_of_locs (curr->expr);
9676 /* If the expression is too large, drop it on the floor. We could
9677 perhaps put it into DW_TAG_dwarf_procedure and refer to that
9678 in the expression, but >= 64KB expressions for a single value
9679 in a single range are unlikely very useful. */
9680 if (dwarf_version < 5 && size > 0xffff)
9681 continue;
9682 if (dwarf_version >= 5)
9683 {
9684 if (dwarf_split_debug_info)
9685 {
9686 /* For -gsplit-dwarf, emit DW_LLE_starx_length, which has
9687 uleb128 index into .debug_addr and uleb128 length. */
9688 dw2_asm_output_data (1, DW_LLE_startx_length,
9689 "DW_LLE_startx_length (%s)",
9690 list_head->ll_symbol);
9691 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
9692 "Location list range start index "
9693 "(%s)", curr->begin);
9694 /* FIXME: This will ICE ifndef HAVE_AS_LEB128.
9695 For that case we probably need to emit DW_LLE_startx_endx,
9696 but we'd need 2 .debug_addr entries rather than just one. */
9697 dw2_asm_output_delta_uleb128 (curr->end, curr->begin,
9698 "Location list length (%s)",
9699 list_head->ll_symbol);
9700 }
9701 else if (!have_multiple_function_sections && HAVE_AS_LEB128)
9702 {
9703 /* If all code is in .text section, the base address is
9704 already provided by the CU attributes. Use
9705 DW_LLE_offset_pair where both addresses are uleb128 encoded
9706 offsets against that base. */
9707 dw2_asm_output_data (1, DW_LLE_offset_pair,
9708 "DW_LLE_offset_pair (%s)",
9709 list_head->ll_symbol);
9710 dw2_asm_output_delta_uleb128 (curr->begin, curr->section,
9711 "Location list begin address (%s)",
9712 list_head->ll_symbol);
9713 dw2_asm_output_delta_uleb128 (curr->end, curr->section,
9714 "Location list end address (%s)",
9715 list_head->ll_symbol);
9716 }
9717 else if (HAVE_AS_LEB128)
9718 {
9719 /* Otherwise, find out how many consecutive entries could share
9720 the same base entry. If just one, emit DW_LLE_start_length,
9721 otherwise emit DW_LLE_base_address for the base address
9722 followed by a series of DW_LLE_offset_pair. */
9723 if (last_section == NULL || curr->section != last_section)
9724 {
9725 dw_loc_list_ref curr2;
9726 for (curr2 = curr->dw_loc_next; curr2 != NULL;
9727 curr2 = curr2->dw_loc_next)
9728 {
9729 if (strcmp (curr2->begin, curr2->end) == 0
9730 && !curr2->force)
9731 continue;
9732 break;
9733 }
9734 if (curr2 == NULL || curr->section != curr2->section)
9735 last_section = NULL;
9736 else
9737 {
9738 last_section = curr->section;
9739 base_label = curr->begin;
9740 dw2_asm_output_data (1, DW_LLE_base_address,
9741 "DW_LLE_base_address (%s)",
9742 list_head->ll_symbol);
9743 dw2_asm_output_addr (DWARF2_ADDR_SIZE, base_label,
9744 "Base address (%s)",
9745 list_head->ll_symbol);
9746 }
9747 }
9748 /* Only one entry with the same base address. Use
9749 DW_LLE_start_length with absolute address and uleb128
9750 length. */
9751 if (last_section == NULL)
9752 {
9753 dw2_asm_output_data (1, DW_LLE_start_length,
9754 "DW_LLE_start_length (%s)",
9755 list_head->ll_symbol);
9756 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
9757 "Location list begin address (%s)",
9758 list_head->ll_symbol);
9759 dw2_asm_output_delta_uleb128 (curr->end, curr->begin,
9760 "Location list length "
9761 "(%s)", list_head->ll_symbol);
9762 }
9763 /* Otherwise emit DW_LLE_offset_pair, relative to above emitted
9764 DW_LLE_base_address. */
9765 else
9766 {
9767 dw2_asm_output_data (1, DW_LLE_offset_pair,
9768 "DW_LLE_offset_pair (%s)",
9769 list_head->ll_symbol);
9770 dw2_asm_output_delta_uleb128 (curr->begin, base_label,
9771 "Location list begin address "
9772 "(%s)", list_head->ll_symbol);
9773 dw2_asm_output_delta_uleb128 (curr->end, base_label,
9774 "Location list end address "
9775 "(%s)", list_head->ll_symbol);
9776 }
9777 }
9778 /* The assembler does not support .uleb128 directive. Emit
9779 DW_LLE_start_end with a pair of absolute addresses. */
9780 else
9781 {
9782 dw2_asm_output_data (1, DW_LLE_start_end,
9783 "DW_LLE_start_end (%s)",
9784 list_head->ll_symbol);
9785 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
9786 "Location list begin address (%s)",
9787 list_head->ll_symbol);
9788 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
9789 "Location list end address (%s)",
9790 list_head->ll_symbol);
9791 }
9792 }
9793 else if (dwarf_split_debug_info)
9794 {
9795 /* For -gsplit-dwarf -gdwarf-{2,3,4} emit index into .debug_addr
9796 and 4 byte length. */
9797 dw2_asm_output_data (1, DW_LLE_GNU_start_length_entry,
9798 "Location list start/length entry (%s)",
9799 list_head->ll_symbol);
9800 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
9801 "Location list range start index (%s)",
9802 curr->begin);
9803 /* The length field is 4 bytes. If we ever need to support
9804 an 8-byte length, we can add a new DW_LLE code or fall back
9805 to DW_LLE_GNU_start_end_entry. */
9806 dw2_asm_output_delta (4, curr->end, curr->begin,
9807 "Location list range length (%s)",
9808 list_head->ll_symbol);
9809 }
9810 else if (!have_multiple_function_sections)
9811 {
9812 /* Pair of relative addresses against start of text section. */
9813 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
9814 "Location list begin address (%s)",
9815 list_head->ll_symbol);
9816 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
9817 "Location list end address (%s)",
9818 list_head->ll_symbol);
9819 }
9820 else
9821 {
9822 /* Pair of absolute addresses. */
9823 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
9824 "Location list begin address (%s)",
9825 list_head->ll_symbol);
9826 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
9827 "Location list end address (%s)",
9828 list_head->ll_symbol);
9829 }
9830
9831 /* Output the block length for this list of location operations. */
9832 if (dwarf_version >= 5)
9833 dw2_asm_output_data_uleb128 (size, "Location expression size");
9834 else
9835 {
9836 gcc_assert (size <= 0xffff);
9837 dw2_asm_output_data (2, size, "Location expression size");
9838 }
9839
9840 output_loc_sequence (curr->expr, -1);
9841 }
9842
9843 /* And finally list termination. */
9844 if (dwarf_version >= 5)
9845 dw2_asm_output_data (1, DW_LLE_end_of_list,
9846 "DW_LLE_end_of_list (%s)", list_head->ll_symbol);
9847 else if (dwarf_split_debug_info)
9848 dw2_asm_output_data (1, DW_LLE_GNU_end_of_list_entry,
9849 "Location list terminator (%s)",
9850 list_head->ll_symbol);
9851 else
9852 {
9853 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
9854 "Location list terminator begin (%s)",
9855 list_head->ll_symbol);
9856 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
9857 "Location list terminator end (%s)",
9858 list_head->ll_symbol);
9859 }
9860}
9861
9862/* Output a range_list offset into the .debug_ranges or .debug_rnglists
9863 section. Emit a relocated reference if val_entry is NULL, otherwise,
9864 emit an indirect reference. */
9865
9866static void
9867output_range_list_offset (dw_attr_node *a)
9868{
9869 const char *name = dwarf_attr_name (a->dw_attr);
9870
9871 if (a->dw_attr_val.val_entry == RELOCATED_OFFSET)
9872 {
9873 if (dwarf_version >= 5)
9874 {
9875 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
9876 dw2_asm_output_offset (DWARF_OFFSET_SIZE, r->label,
9877 debug_ranges_section, "%s", name);
9878 }
9879 else
9880 {
9881 char *p = strchr (ranges_section_label, '\0');
9882 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
9883 a->dw_attr_val.v.val_offset * 2 * DWARF2_ADDR_SIZE);
9884 dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
9885 debug_ranges_section, "%s", name);
9886 *p = '\0';
9887 }
9888 }
9889 else if (dwarf_version >= 5)
9890 {
9891 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
9892 gcc_assert (rnglist_idx);
9893 dw2_asm_output_data_uleb128 (r->idx, "%s", name);
9894 }
9895 else
9896 dw2_asm_output_data (DWARF_OFFSET_SIZE,
9897 a->dw_attr_val.v.val_offset * 2 * DWARF2_ADDR_SIZE,
9898 "%s (offset from %s)", name, ranges_section_label);
9899}
9900
9901/* Output the offset into the debug_loc section. */
9902
9903static void
9904output_loc_list_offset (dw_attr_node *a)
9905{
9906 char *sym = AT_loc_list (a)->ll_symbol;
9907
9908 gcc_assert (sym);
9909 if (!dwarf_split_debug_info)
9910 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
9911 "%s", dwarf_attr_name (a->dw_attr));
9912 else if (dwarf_version >= 5)
9913 {
9914 gcc_assert (AT_loc_list (a)->num_assigned);
9915 dw2_asm_output_data_uleb128 (AT_loc_list (a)->hash, "%s (%s)",
9916 dwarf_attr_name (a->dw_attr),
9917 sym);
9918 }
9919 else
9920 dw2_asm_output_delta (DWARF_OFFSET_SIZE, sym, loc_section_label,
9921 "%s", dwarf_attr_name (a->dw_attr));
9922}
9923
9924/* Output an attribute's index or value appropriately. */
9925
9926static void
9927output_attr_index_or_value (dw_attr_node *a)
9928{
9929 const char *name = dwarf_attr_name (a->dw_attr);
9930
9931 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
9932 {
9933 dw2_asm_output_data_uleb128 (AT_index (a), "%s", name);
9934 return;
9935 }
9936 switch (AT_class (a))
9937 {
9938 case dw_val_class_addr:
9939 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
9940 break;
9941 case dw_val_class_high_pc:
9942 case dw_val_class_lbl_id:
9943 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
9944 break;
9945 default:
9946 gcc_unreachable ();
9947 }
9948}
9949
9950/* Output a type signature. */
9951
9952static inline void
9953output_signature (const char *sig, const char *name)
9954{
9955 int i;
9956
9957 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9958 dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
9959}
9960
9961/* Output a discriminant value. */
9962
9963static inline void
9964output_discr_value (dw_discr_value *discr_value, const char *name)
9965{
9966 if (discr_value->pos)
9967 dw2_asm_output_data_uleb128 (discr_value->v.uval, "%s", name);
9968 else
9969 dw2_asm_output_data_sleb128 (discr_value->v.sval, "%s", name);
9970}
9971
9972/* Output the DIE and its attributes. Called recursively to generate
9973 the definitions of each child DIE. */
9974
9975static void
9976output_die (dw_die_ref die)
9977{
9978 dw_attr_node *a;
9979 dw_die_ref c;
9980 unsigned long size;
9981 unsigned ix;
9982
9983 /* If someone in another CU might refer to us, set up a symbol for
9984 them to point to. */
9985 if (! die->comdat_type_p && die->die_id.die_symbol)
9986 output_die_symbol (die);
9987
9988 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
9989 (unsigned long)die->die_offset,
9990 dwarf_tag_name (die->die_tag));
9991
9992 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9993 {
9994 const char *name = dwarf_attr_name (a->dw_attr);
9995
9996 switch (AT_class (a))
9997 {
9998 case dw_val_class_addr:
9999 output_attr_index_or_value (a);
10000 break;
10001
10002 case dw_val_class_offset:
10003 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
10004 "%s", name);
10005 break;
10006
10007 case dw_val_class_range_list:
10008 output_range_list_offset (a);
10009 break;
10010
10011 case dw_val_class_loc:
10012 size = size_of_locs (AT_loc (a));
10013
10014 /* Output the block length for this list of location operations. */
10015 if (dwarf_version >= 4)
10016 dw2_asm_output_data_uleb128 (size, "%s", name);
10017 else
10018 dw2_asm_output_data (constant_size (size), size, "%s", name);
10019
10020 output_loc_sequence (AT_loc (a), -1);
10021 break;
10022
10023 case dw_val_class_const:
10024 /* ??? It would be slightly more efficient to use a scheme like is
10025 used for unsigned constants below, but gdb 4.x does not sign
10026 extend. Gdb 5.x does sign extend. */
10027 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
10028 break;
10029
10030 case dw_val_class_unsigned_const:
10031 {
10032 int csize = constant_size (AT_unsigned (a));
10033 if (dwarf_version == 3
10034 && a->dw_attr == DW_AT_data_member_location
10035 && csize >= 4)
10036 dw2_asm_output_data_uleb128 (AT_unsigned (a), "%s", name);
10037 else
10038 dw2_asm_output_data (csize, AT_unsigned (a), "%s", name);
10039 }
10040 break;
10041
10042 case dw_val_class_const_implicit:
10043 if (flag_debug_asm)
10044 fprintf (asm_out_file, "\t\t\t%s %s ("
10045 HOST_WIDE_INT_PRINT_DEC ")\n",
10046 ASM_COMMENT_START, name, AT_int (a));
10047 break;
10048
10049 case dw_val_class_unsigned_const_implicit:
10050 if (flag_debug_asm)
10051 fprintf (asm_out_file, "\t\t\t%s %s ("
10052 HOST_WIDE_INT_PRINT_HEX ")\n",
10053 ASM_COMMENT_START, name, AT_unsigned (a));
10054 break;
10055
10056 case dw_val_class_const_double:
10057 {
10058 unsigned HOST_WIDE_INT first, second;
10059
10060 if (HOST_BITS_PER_WIDE_INT >= DWARF_LARGEST_DATA_FORM_BITS)
10061 dw2_asm_output_data (1,
10062 HOST_BITS_PER_DOUBLE_INT
10063 / HOST_BITS_PER_CHAR,
10064 NULL);
10065
10066 if (WORDS_BIG_ENDIAN)
10067 {
10068 first = a->dw_attr_val.v.val_double.high;
10069 second = a->dw_attr_val.v.val_double.low;
10070 }
10071 else
10072 {
10073 first = a->dw_attr_val.v.val_double.low;
10074 second = a->dw_attr_val.v.val_double.high;
10075 }
10076
10077 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
10078 first, "%s", name);
10079 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
10080 second, NULL);
10081 }
10082 break;
10083
10084 case dw_val_class_wide_int:
10085 {
10086 int i;
10087 int len = get_full_len (*a->dw_attr_val.v.val_wide);
10088 int l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
10089 if (len * HOST_BITS_PER_WIDE_INT > DWARF_LARGEST_DATA_FORM_BITS)
10090 dw2_asm_output_data (1, get_full_len (*a->dw_attr_val.v.val_wide)
10091 * l, NULL);
10092
10093 if (WORDS_BIG_ENDIAN)
10094 for (i = len - 1; i >= 0; --i)
10095 {
10096 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
10097 "%s", name);
10098 name = "";
10099 }
10100 else
10101 for (i = 0; i < len; ++i)
10102 {
10103 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
10104 "%s", name);
10105 name = "";
10106 }
10107 }
10108 break;
10109
10110 case dw_val_class_vec:
10111 {
10112 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
10113 unsigned int len = a->dw_attr_val.v.val_vec.length;
10114 unsigned int i;
10115 unsigned char *p;
10116
10117 dw2_asm_output_data (constant_size (len * elt_size),
10118 len * elt_size, "%s", name);
10119 if (elt_size > sizeof (HOST_WIDE_INT))
10120 {
10121 elt_size /= 2;
10122 len *= 2;
10123 }
10124 for (i = 0, p = (unsigned char *) a->dw_attr_val.v.val_vec.array;
10125 i < len;
10126 i++, p += elt_size)
10127 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
10128 "fp or vector constant word %u", i);
10129 break;
10130 }
10131
10132 case dw_val_class_flag:
10133 if (dwarf_version >= 4)
10134 {
10135 /* Currently all add_AT_flag calls pass in 1 as last argument,
10136 so DW_FORM_flag_present can be used. If that ever changes,
10137 we'll need to use DW_FORM_flag and have some optimization
10138 in build_abbrev_table that will change those to
10139 DW_FORM_flag_present if it is set to 1 in all DIEs using
10140 the same abbrev entry. */
10141 gcc_assert (AT_flag (a) == 1);
10142 if (flag_debug_asm)
10143 fprintf (asm_out_file, "\t\t\t%s %s\n",
10144 ASM_COMMENT_START, name);
10145 break;
10146 }
10147 dw2_asm_output_data (1, AT_flag (a), "%s", name);
10148 break;
10149
10150 case dw_val_class_loc_list:
10151 output_loc_list_offset (a);
10152 break;
10153
10154 case dw_val_class_die_ref:
10155 if (AT_ref_external (a))
10156 {
10157 if (AT_ref (a)->comdat_type_p)
10158 {
10159 comdat_type_node *type_node
10160 = AT_ref (a)->die_id.die_type_node;
10161
10162 gcc_assert (type_node);
10163 output_signature (type_node->signature, name);
10164 }
10165 else
10166 {
10167 const char *sym = AT_ref (a)->die_id.die_symbol;
10168 int size;
10169
10170 gcc_assert (sym);
10171 /* In DWARF2, DW_FORM_ref_addr is sized by target address
10172 length, whereas in DWARF3 it's always sized as an
10173 offset. */
10174 if (dwarf_version == 2)
10175 size = DWARF2_ADDR_SIZE;
10176 else
10177 size = DWARF_OFFSET_SIZE;
10178 dw2_asm_output_offset (size, sym, debug_info_section, "%s",
10179 name);
10180 }
10181 }
10182 else
10183 {
10184 gcc_assert (AT_ref (a)->die_offset);
10185 dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
10186 "%s", name);
10187 }
10188 break;
10189
10190 case dw_val_class_fde_ref:
10191 {
10192 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
10193
10194 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
10195 a->dw_attr_val.v.val_fde_index * 2);
10196 dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
10197 "%s", name);
10198 }
10199 break;
10200
10201 case dw_val_class_vms_delta:
10202#ifdef ASM_OUTPUT_DWARF_VMS_DELTA
10203 dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
10204 AT_vms_delta2 (a), AT_vms_delta1 (a),
10205 "%s", name);
10206#else
10207 dw2_asm_output_delta (DWARF_OFFSET_SIZE,
10208 AT_vms_delta2 (a), AT_vms_delta1 (a),
10209 "%s", name);
10210#endif
10211 break;
10212
10213 case dw_val_class_lbl_id:
10214 output_attr_index_or_value (a);
10215 break;
10216
10217 case dw_val_class_lineptr:
10218 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
10219 debug_line_section, "%s", name);
10220 break;
10221
10222 case dw_val_class_macptr:
10223 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
10224 debug_macinfo_section, "%s", name);
10225 break;
10226
10227 case dw_val_class_loclistsptr:
10228 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
10229 debug_loc_section, "%s", name);
10230 break;
10231
10232 case dw_val_class_str:
10233 if (a->dw_attr_val.v.val_str->form == DW_FORM_strp)
10234 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
10235 a->dw_attr_val.v.val_str->label,
10236 debug_str_section,
10237 "%s: \"%s\"", name, AT_string (a));
10238 else if (a->dw_attr_val.v.val_str->form == DW_FORM_line_strp)
10239 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
10240 a->dw_attr_val.v.val_str->label,
10241 debug_line_str_section,
10242 "%s: \"%s\"", name, AT_string (a));
10243 else if (a->dw_attr_val.v.val_str->form == DW_FORM_GNU_str_index)
10244 dw2_asm_output_data_uleb128 (AT_index (a),
10245 "%s: \"%s\"", name, AT_string (a));
10246 else
10247 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
10248 break;
10249
10250 case dw_val_class_file:
10251 {
10252 int f = maybe_emit_file (a->dw_attr_val.v.val_file);
10253
10254 dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
10255 a->dw_attr_val.v.val_file->filename);
10256 break;
10257 }
10258
10259 case dw_val_class_file_implicit:
10260 if (flag_debug_asm)
10261 fprintf (asm_out_file, "\t\t\t%s %s (%d, %s)\n",
10262 ASM_COMMENT_START, name,
10263 maybe_emit_file (a->dw_attr_val.v.val_file),
10264 a->dw_attr_val.v.val_file->filename);
10265 break;
10266
10267 case dw_val_class_data8:
10268 {
10269 int i;
10270
10271 for (i = 0; i < 8; i++)
10272 dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
10273 i == 0 ? "%s" : NULL, name);
10274 break;
10275 }
10276
10277 case dw_val_class_high_pc:
10278 dw2_asm_output_delta (DWARF2_ADDR_SIZE, AT_lbl (a),
10279 get_AT_low_pc (die), "DW_AT_high_pc");
10280 break;
10281
10282 case dw_val_class_discr_value:
10283 output_discr_value (&a->dw_attr_val.v.val_discr_value, name);
10284 break;
10285
10286 case dw_val_class_discr_list:
10287 {
10288 dw_discr_list_ref list = AT_discr_list (a);
10289 const int size = size_of_discr_list (list);
10290
10291 /* This is a block, so output its length first. */
10292 dw2_asm_output_data (constant_size (size), size,
10293 "%s: block size", name);
10294
10295 for (; list != NULL; list = list->dw_discr_next)
10296 {
10297 /* One byte for the discriminant value descriptor, and then as
10298 many LEB128 numbers as required. */
10299 if (list->dw_discr_range)
10300 dw2_asm_output_data (1, DW_DSC_range,
10301 "%s: DW_DSC_range", name);
10302 else
10303 dw2_asm_output_data (1, DW_DSC_label,
10304 "%s: DW_DSC_label", name);
10305
10306 output_discr_value (&list->dw_discr_lower_bound, name);
10307 if (list->dw_discr_range)
10308 output_discr_value (&list->dw_discr_upper_bound, name);
10309 }
10310 break;
10311 }
10312
10313 default:
10314 gcc_unreachable ();
10315 }
10316 }
10317
10318 FOR_EACH_CHILD (die, c, output_die (c));
10319
10320 /* Add null byte to terminate sibling list. */
10321 if (die->die_child != NULL)
10322 dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
10323 (unsigned long) die->die_offset);
10324}
10325
10326/* Output the compilation unit that appears at the beginning of the
10327 .debug_info section, and precedes the DIE descriptions. */
10328
10329static void
10330output_compilation_unit_header (enum dwarf_unit_type ut)
10331{
10332 if (!XCOFF_DEBUGGING_INFO)
10333 {
10334 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10335 dw2_asm_output_data (4, 0xffffffff,
10336 "Initial length escape value indicating 64-bit DWARF extension");
10337 dw2_asm_output_data (DWARF_OFFSET_SIZE,
10338 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
10339 "Length of Compilation Unit Info");
10340 }
10341
10342 dw2_asm_output_data (2, dwarf_version, "DWARF version number");
10343 if (dwarf_version >= 5)
10344 {
10345 const char *name;
10346 switch (ut)
10347 {
10348 case DW_UT_compile: name = "DW_UT_compile"; break;
10349 case DW_UT_type: name = "DW_UT_type"; break;
10350 case DW_UT_split_compile: name = "DW_UT_split_compile"; break;
10351 case DW_UT_split_type: name = "DW_UT_split_type"; break;
10352 default: gcc_unreachable ();
10353 }
10354 dw2_asm_output_data (1, ut, "%s", name);
10355 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
10356 }
10357 dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
10358 debug_abbrev_section,
10359 "Offset Into Abbrev. Section");
10360 if (dwarf_version < 5)
10361 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
10362}
10363
10364/* Output the compilation unit DIE and its children. */
10365
10366static void
10367output_comp_unit (dw_die_ref die, int output_if_empty,
10368 const unsigned char *dwo_id)
10369{
10370 const char *secname, *oldsym;
10371 char *tmp;
10372
10373 /* Unless we are outputting main CU, we may throw away empty ones. */
10374 if (!output_if_empty && die->die_child == NULL)
10375 return;
10376
10377 /* Even if there are no children of this DIE, we must output the information
10378 about the compilation unit. Otherwise, on an empty translation unit, we
10379 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
10380 will then complain when examining the file. First mark all the DIEs in
10381 this CU so we know which get local refs. */
10382 mark_dies (die);
10383
10384 external_ref_hash_type *extern_map = optimize_external_refs (die);
10385
10386 /* For now, optimize only the main CU, in order to optimize the rest
10387 we'd need to see all of them earlier. Leave the rest for post-linking
10388 tools like DWZ. */
10389 if (die == comp_unit_die ())
10390 abbrev_opt_start = vec_safe_length (abbrev_die_table);
10391
10392 build_abbrev_table (die, extern_map);
10393
10394 optimize_abbrev_table ();
10395
10396 delete extern_map;
10397
10398 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
10399 next_die_offset = (dwo_id
10400 ? DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE
10401 : DWARF_COMPILE_UNIT_HEADER_SIZE);
10402 calc_die_sizes (die);
10403
10404 oldsym = die->die_id.die_symbol;
10405 if (oldsym)
10406 {
10407 tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
10408
10409 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
10410 secname = tmp;
10411 die->die_id.die_symbol = NULL;
10412 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
10413 }
10414 else
10415 {
10416 switch_to_section (debug_info_section);
10417 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
10418 info_section_emitted = true;
10419 }
10420
10421 /* Output debugging information. */
10422 output_compilation_unit_header (dwo_id
10423 ? DW_UT_split_compile : DW_UT_compile);
10424 if (dwarf_version >= 5)
10425 {
10426 if (dwo_id != NULL)
10427 for (int i = 0; i < 8; i++)
10428 dw2_asm_output_data (1, dwo_id[i], i == 0 ? "DWO id" : NULL);
10429 }
10430 output_die (die);
10431
10432 /* Leave the marks on the main CU, so we can check them in
10433 output_pubnames. */
10434 if (oldsym)
10435 {
10436 unmark_dies (die);
10437 die->die_id.die_symbol = oldsym;
10438 }
10439}
10440
10441/* Whether to generate the DWARF accelerator tables in .debug_pubnames
10442 and .debug_pubtypes. This is configured per-target, but can be
10443 overridden by the -gpubnames or -gno-pubnames options. */
10444
10445static inline bool
10446want_pubnames (void)
10447{
10448 if (debug_info_level <= DINFO_LEVEL_TERSE)
10449 return false;
10450 if (debug_generate_pub_sections != -1)
10451 return debug_generate_pub_sections;
10452 return targetm.want_debug_pub_sections;
10453}
10454
10455/* Add the DW_AT_GNU_pubnames and DW_AT_GNU_pubtypes attributes. */
10456
10457static void
10458add_AT_pubnames (dw_die_ref die)
10459{
10460 if (want_pubnames ())
10461 add_AT_flag (die, DW_AT_GNU_pubnames, 1);
10462}
10463
10464/* Add a string attribute value to a skeleton DIE. */
10465
10466static inline void
10467add_skeleton_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind,
10468 const char *str)
10469{
10470 dw_attr_node attr;
10471 struct indirect_string_node *node;
10472
10473 if (! skeleton_debug_str_hash)
10474 skeleton_debug_str_hash
10475 = hash_table<indirect_string_hasher>::create_ggc (10);
10476
10477 node = find_AT_string_in_table (str, skeleton_debug_str_hash);
10478 find_string_form (node);
10479 if (node->form == DW_FORM_GNU_str_index)
10480 node->form = DW_FORM_strp;
10481
10482 attr.dw_attr = attr_kind;
10483 attr.dw_attr_val.val_class = dw_val_class_str;
10484 attr.dw_attr_val.val_entry = NULL;
10485 attr.dw_attr_val.v.val_str = node;
10486 add_dwarf_attr (die, &attr);
10487}
10488
10489/* Helper function to generate top-level dies for skeleton debug_info and
10490 debug_types. */
10491
10492static void
10493add_top_level_skeleton_die_attrs (dw_die_ref die)
10494{
10495 const char *dwo_file_name = concat (aux_base_name, ".dwo", NULL);
10496 const char *comp_dir = comp_dir_string ();
10497
10498 add_skeleton_AT_string (die, dwarf_AT (DW_AT_dwo_name), dwo_file_name);
10499 if (comp_dir != NULL)
10500 add_skeleton_AT_string (die, DW_AT_comp_dir, comp_dir);
10501 add_AT_pubnames (die);
10502 add_AT_lineptr (die, DW_AT_GNU_addr_base, debug_addr_section_label);
10503}
10504
10505/* Output skeleton debug sections that point to the dwo file. */
10506
10507static void
10508output_skeleton_debug_sections (dw_die_ref comp_unit,
10509 const unsigned char *dwo_id)
10510{
10511 /* These attributes will be found in the full debug_info section. */
10512 remove_AT (comp_unit, DW_AT_producer);
10513 remove_AT (comp_unit, DW_AT_language);
10514
10515 switch_to_section (debug_skeleton_info_section);
10516 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_info_section_label);
10517
10518 /* Produce the skeleton compilation-unit header. This one differs enough from
10519 a normal CU header that it's better not to call output_compilation_unit
10520 header. */
10521 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10522 dw2_asm_output_data (4, 0xffffffff,
10523 "Initial length escape value indicating 64-bit "
10524 "DWARF extension");
10525
10526 dw2_asm_output_data (DWARF_OFFSET_SIZE,
10527 DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE
10528 - DWARF_INITIAL_LENGTH_SIZE
10529 + size_of_die (comp_unit),
10530 "Length of Compilation Unit Info");
10531 dw2_asm_output_data (2, dwarf_version, "DWARF version number");
10532 if (dwarf_version >= 5)
10533 {
10534 dw2_asm_output_data (1, DW_UT_skeleton, "DW_UT_skeleton");
10535 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
10536 }
10537 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_abbrev_section_label,
10538 debug_skeleton_abbrev_section,
10539 "Offset Into Abbrev. Section");
10540 if (dwarf_version < 5)
10541 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
10542 else
10543 for (int i = 0; i < 8; i++)
10544 dw2_asm_output_data (1, dwo_id[i], i == 0 ? "DWO id" : NULL);
10545
10546 comp_unit->die_abbrev = SKELETON_COMP_DIE_ABBREV;
10547 output_die (comp_unit);
10548
10549 /* Build the skeleton debug_abbrev section. */
10550 switch_to_section (debug_skeleton_abbrev_section);
10551 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_abbrev_section_label);
10552
10553 output_die_abbrevs (SKELETON_COMP_DIE_ABBREV, comp_unit);
10554
10555 dw2_asm_output_data (1, 0, "end of skeleton .debug_abbrev");
10556}
10557
10558/* Output a comdat type unit DIE and its children. */
10559
10560static void
10561output_comdat_type_unit (comdat_type_node *node)
10562{
10563 const char *secname;
10564 char *tmp;
10565 int i;
10566#if defined (OBJECT_FORMAT_ELF)
10567 tree comdat_key;
10568#endif
10569
10570 /* First mark all the DIEs in this CU so we know which get local refs. */
10571 mark_dies (node->root_die);
10572
10573 external_ref_hash_type *extern_map = optimize_external_refs (node->root_die);
10574
10575 build_abbrev_table (node->root_die, extern_map);
10576
10577 delete extern_map;
10578 extern_map = NULL;
10579
10580 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
10581 next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
10582 calc_die_sizes (node->root_die);
10583
10584#if defined (OBJECT_FORMAT_ELF)
10585 if (dwarf_version >= 5)
10586 {
10587 if (!dwarf_split_debug_info)
10588 secname = ".debug_info";
10589 else
10590 secname = ".debug_info.dwo";
10591 }
10592 else if (!dwarf_split_debug_info)
10593 secname = ".debug_types";
10594 else
10595 secname = ".debug_types.dwo";
10596
10597 tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
10598 sprintf (tmp, dwarf_version >= 5 ? "wi." : "wt.");
10599 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
10600 sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
10601 comdat_key = get_identifier (tmp);
10602 targetm.asm_out.named_section (secname,
10603 SECTION_DEBUG | SECTION_LINKONCE,
10604 comdat_key);
10605#else
10606 tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
10607 sprintf (tmp, (dwarf_version >= 5
10608 ? ".gnu.linkonce.wi." : ".gnu.linkonce.wt."));
10609 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
10610 sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
10611 secname = tmp;
10612 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
10613#endif
10614
10615 /* Output debugging information. */
10616 output_compilation_unit_header (dwarf_split_debug_info
10617 ? DW_UT_split_type : DW_UT_type);
10618 output_signature (node->signature, "Type Signature");
10619 dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
10620 "Offset to Type DIE");
10621 output_die (node->root_die);
10622
10623 unmark_dies (node->root_die);
10624}
10625
10626/* Return the DWARF2/3 pubname associated with a decl. */
10627
10628static const char *
10629dwarf2_name (tree decl, int scope)
10630{
10631 if (DECL_NAMELESS (decl))
10632 return NULL;
10633 return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
10634}
10635
10636/* Add a new entry to .debug_pubnames if appropriate. */
10637
10638static void
10639add_pubname_string (const char *str, dw_die_ref die)
10640{
10641 pubname_entry e;
10642
10643 e.die = die;
10644 e.name = xstrdup (str);
10645 vec_safe_push (pubname_table, e);
10646}
10647
10648static void
10649add_pubname (tree decl, dw_die_ref die)
10650{
10651 if (!want_pubnames ())
10652 return;
10653
10654 /* Don't add items to the table when we expect that the consumer will have
10655 just read the enclosing die. For example, if the consumer is looking at a
10656 class_member, it will either be inside the class already, or will have just
10657 looked up the class to find the member. Either way, searching the class is
10658 faster than searching the index. */
10659 if ((TREE_PUBLIC (decl) && !class_scope_p (die->die_parent))
10660 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
10661 {
10662 const char *name = dwarf2_name (decl, 1);
10663
10664 if (name)
10665 add_pubname_string (name, die);
10666 }
10667}
10668
10669/* Add an enumerator to the pubnames section. */
10670
10671static void
10672add_enumerator_pubname (const char *scope_name, dw_die_ref die)
10673{
10674 pubname_entry e;
10675
10676 gcc_assert (scope_name);
10677 e.name = concat (scope_name, get_AT_string (die, DW_AT_name), NULL);
10678 e.die = die;
10679 vec_safe_push (pubname_table, e);
10680}
10681
10682/* Add a new entry to .debug_pubtypes if appropriate. */
10683
10684static void
10685add_pubtype (tree decl, dw_die_ref die)
10686{
10687 pubname_entry e;
10688
10689 if (!want_pubnames ())
10690 return;
10691
10692 if ((TREE_PUBLIC (decl)
10693 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
10694 && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
10695 {
10696 tree scope = NULL;
10697 const char *scope_name = "";
10698 const char *sep = is_cxx () ? "::" : ".";
10699 const char *name;
10700
10701 scope = TYPE_P (decl) ? TYPE_CONTEXT (decl) : NULL;
10702 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
10703 {
10704 scope_name = lang_hooks.dwarf_name (scope, 1);
10705 if (scope_name != NULL && scope_name[0] != '\0')
10706 scope_name = concat (scope_name, sep, NULL);
10707 else
10708 scope_name = "";
10709 }
10710
10711 if (TYPE_P (decl))
10712 name = type_tag (decl);
10713 else
10714 name = lang_hooks.dwarf_name (decl, 1);
10715
10716 /* If we don't have a name for the type, there's no point in adding
10717 it to the table. */
10718 if (name != NULL && name[0] != '\0')
10719 {
10720 e.die = die;
10721 e.name = concat (scope_name, name, NULL);
10722 vec_safe_push (pubtype_table, e);
10723 }
10724
10725 /* Although it might be more consistent to add the pubinfo for the
10726 enumerators as their dies are created, they should only be added if the
10727 enum type meets the criteria above. So rather than re-check the parent
10728 enum type whenever an enumerator die is created, just output them all
10729 here. This isn't protected by the name conditional because anonymous
10730 enums don't have names. */
10731 if (die->die_tag == DW_TAG_enumeration_type)
10732 {
10733 dw_die_ref c;
10734
10735 FOR_EACH_CHILD (die, c, add_enumerator_pubname (scope_name, c));
10736 }
10737 }
10738}
10739
10740/* Output a single entry in the pubnames table. */
10741
10742static void
10743output_pubname (dw_offset die_offset, pubname_entry *entry)
10744{
10745 dw_die_ref die = entry->die;
10746 int is_static = get_AT_flag (die, DW_AT_external) ? 0 : 1;
10747
10748 dw2_asm_output_data (DWARF_OFFSET_SIZE, die_offset, "DIE offset");
10749
10750 if (debug_generate_pub_sections == 2)
10751 {
10752 /* This logic follows gdb's method for determining the value of the flag
10753 byte. */
10754 uint32_t flags = GDB_INDEX_SYMBOL_KIND_NONE;
10755 switch (die->die_tag)
10756 {
10757 case DW_TAG_typedef:
10758 case DW_TAG_base_type:
10759 case DW_TAG_subrange_type:
10760 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
10761 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
10762 break;
10763 case DW_TAG_enumerator:
10764 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
10765 GDB_INDEX_SYMBOL_KIND_VARIABLE);
10766 if (!is_cxx () && !is_java ())
10767 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
10768 break;
10769 case DW_TAG_subprogram:
10770 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
10771 GDB_INDEX_SYMBOL_KIND_FUNCTION);
10772 if (!is_ada ())
10773 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
10774 break;
10775 case DW_TAG_constant:
10776 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
10777 GDB_INDEX_SYMBOL_KIND_VARIABLE);
10778 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
10779 break;
10780 case DW_TAG_variable:
10781 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
10782 GDB_INDEX_SYMBOL_KIND_VARIABLE);
10783 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
10784 break;
10785 case DW_TAG_namespace:
10786 case DW_TAG_imported_declaration:
10787 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
10788 break;
10789 case DW_TAG_class_type:
10790 case DW_TAG_interface_type:
10791 case DW_TAG_structure_type:
10792 case DW_TAG_union_type:
10793 case DW_TAG_enumeration_type:
10794 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
10795 if (!is_cxx () && !is_java ())
10796 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
10797 break;
10798 default:
10799 /* An unusual tag. Leave the flag-byte empty. */
10800 break;
10801 }
10802 dw2_asm_output_data (1, flags >> GDB_INDEX_CU_BITSIZE,
10803 "GDB-index flags");
10804 }
10805
10806 dw2_asm_output_nstring (entry->name, -1, "external name");
10807}
10808
10809
10810/* Output the public names table used to speed up access to externally
10811 visible names; or the public types table used to find type definitions. */
10812
10813static void
10814output_pubnames (vec<pubname_entry, va_gc> *names)
10815{
10816 unsigned i;
10817 unsigned long pubnames_length = size_of_pubnames (names);
10818 pubname_entry *pub;
10819
10820 if (!XCOFF_DEBUGGING_INFO)
10821 {
10822 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10823 dw2_asm_output_data (4, 0xffffffff,
10824 "Initial length escape value indicating 64-bit DWARF extension");
10825 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
10826 "Pub Info Length");
10827 }
10828
10829 /* Version number for pubnames/pubtypes is independent of dwarf version. */
10830 dw2_asm_output_data (2, 2, "DWARF Version");
10831
10832 if (dwarf_split_debug_info)
10833 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
10834 debug_skeleton_info_section,
10835 "Offset of Compilation Unit Info");
10836 else
10837 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
10838 debug_info_section,
10839 "Offset of Compilation Unit Info");
10840 dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
10841 "Compilation Unit Length");
10842
10843 FOR_EACH_VEC_ELT (*names, i, pub)
10844 {
10845 if (include_pubname_in_output (names, pub))
10846 {
10847 dw_offset die_offset = pub->die->die_offset;
10848
10849 /* We shouldn't see pubnames for DIEs outside of the main CU. */
10850 if (names == pubname_table && pub->die->die_tag != DW_TAG_enumerator)
10851 gcc_assert (pub->die->die_mark);
10852
10853 /* If we're putting types in their own .debug_types sections,
10854 the .debug_pubtypes table will still point to the compile
10855 unit (not the type unit), so we want to use the offset of
10856 the skeleton DIE (if there is one). */
10857 if (pub->die->comdat_type_p && names == pubtype_table)
10858 {
10859 comdat_type_node *type_node = pub->die->die_id.die_type_node;
10860
10861 if (type_node != NULL)
10862 die_offset = (type_node->skeleton_die != NULL
10863 ? type_node->skeleton_die->die_offset
10864 : comp_unit_die ()->die_offset);
10865 }
10866
10867 output_pubname (die_offset, pub);
10868 }
10869 }
10870
10871 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
10872}
10873
10874/* Output public names and types tables if necessary. */
10875
10876static void
10877output_pubtables (void)
10878{
10879 if (!want_pubnames () || !info_section_emitted)
10880 return;
10881
10882 switch_to_section (debug_pubnames_section);
10883 output_pubnames (pubname_table);
10884 /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
10885 It shouldn't hurt to emit it always, since pure DWARF2 consumers
10886 simply won't look for the section. */
10887 switch_to_section (debug_pubtypes_section);
10888 output_pubnames (pubtype_table);
10889}
10890
10891
10892/* Output the information that goes into the .debug_aranges table.
10893 Namely, define the beginning and ending address range of the
10894 text section generated for this compilation unit. */
10895
10896static void
10897output_aranges (void)
10898{
10899 unsigned i;
10900 unsigned long aranges_length = size_of_aranges ();
10901
10902 if (!XCOFF_DEBUGGING_INFO)
10903 {
10904 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10905 dw2_asm_output_data (4, 0xffffffff,
10906 "Initial length escape value indicating 64-bit DWARF extension");
10907 dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
10908 "Length of Address Ranges Info");
10909 }
10910
10911 /* Version number for aranges is still 2, even up to DWARF5. */
10912 dw2_asm_output_data (2, 2, "DWARF Version");
10913 if (dwarf_split_debug_info)
10914 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
10915 debug_skeleton_info_section,
10916 "Offset of Compilation Unit Info");
10917 else
10918 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
10919 debug_info_section,
10920 "Offset of Compilation Unit Info");
10921 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
10922 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
10923
10924 /* We need to align to twice the pointer size here. */
10925 if (DWARF_ARANGES_PAD_SIZE)
10926 {
10927 /* Pad using a 2 byte words so that padding is correct for any
10928 pointer size. */
10929 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
10930 2 * DWARF2_ADDR_SIZE);
10931 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
10932 dw2_asm_output_data (2, 0, NULL);
10933 }
10934
10935 /* It is necessary not to output these entries if the sections were
10936 not used; if the sections were not used, the length will be 0 and
10937 the address may end up as 0 if the section is discarded by ld
10938 --gc-sections, leaving an invalid (0, 0) entry that can be
10939 confused with the terminator. */
10940 if (text_section_used)
10941 {
10942 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
10943 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
10944 text_section_label, "Length");
10945 }
10946 if (cold_text_section_used)
10947 {
10948 dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
10949 "Address");
10950 dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
10951 cold_text_section_label, "Length");
10952 }
10953
10954 if (have_multiple_function_sections)
10955 {
10956 unsigned fde_idx;
10957 dw_fde_ref fde;
10958
10959 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
10960 {
10961 if (DECL_IGNORED_P (fde->decl))
10962 continue;
10963 if (!fde->in_std_section)
10964 {
10965 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
10966 "Address");
10967 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
10968 fde->dw_fde_begin, "Length");
10969 }
10970 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
10971 {
10972 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
10973 "Address");
10974 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
10975 fde->dw_fde_second_begin, "Length");
10976 }
10977 }
10978 }
10979
10980 /* Output the terminator words. */
10981 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
10982 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
10983}
10984
10985/* Add a new entry to .debug_ranges. Return its index into
10986 ranges_table vector. */
10987
10988static unsigned int
10989add_ranges_num (int num, bool maybe_new_sec)
10990{
10991 dw_ranges r = { NULL, num, 0, maybe_new_sec };
10992 vec_safe_push (ranges_table, r);
10993 return vec_safe_length (ranges_table) - 1;
10994}
10995
10996/* Add a new entry to .debug_ranges corresponding to a block, or a
10997 range terminator if BLOCK is NULL. MAYBE_NEW_SEC is true if
10998 this entry might be in a different section from previous range. */
10999
11000static unsigned int
11001add_ranges (const_tree block, bool maybe_new_sec)
11002{
11003 return add_ranges_num (block ? BLOCK_NUMBER (block) : 0, maybe_new_sec);
11004}
11005
11006/* Note that (*rnglist_table)[offset] is either a head of a rnglist
11007 chain, or middle entry of a chain that will be directly referred to. */
11008
11009static void
11010note_rnglist_head (unsigned int offset)
11011{
11012 if (dwarf_version < 5 || (*ranges_table)[offset].label)
11013 return;
11014 (*ranges_table)[offset].label = gen_internal_sym ("LLRL");
11015}
11016
11017/* Add a new entry to .debug_ranges corresponding to a pair of labels.
11018 When using dwarf_split_debug_info, address attributes in dies destined
11019 for the final executable should be direct references--setting the
11020 parameter force_direct ensures this behavior. */
11021
11022static void
11023add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
11024 bool *added, bool force_direct)
11025{
11026 unsigned int in_use = vec_safe_length (ranges_by_label);
11027 unsigned int offset;
11028 dw_ranges_by_label rbl = { begin, end };
11029 vec_safe_push (ranges_by_label, rbl);
11030 offset = add_ranges_num (-(int)in_use - 1, true);
11031 if (!*added)
11032 {
11033 add_AT_range_list (die, DW_AT_ranges, offset, force_direct);
11034 *added = true;
11035 note_rnglist_head (offset);
11036 }
11037}
11038
11039/* Emit .debug_ranges section. */
11040
11041static void
11042output_ranges (void)
11043{
11044 unsigned i;
11045 static const char *const start_fmt = "Offset %#x";
11046 const char *fmt = start_fmt;
11047 dw_ranges *r;
11048
11049 switch_to_section (debug_ranges_section);
11050 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
11051 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11052 {
11053 int block_num = r->num;
11054
11055 if (block_num > 0)
11056 {
11057 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
11058 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
11059
11060 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
11061 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
11062
11063 /* If all code is in the text section, then the compilation
11064 unit base address defaults to DW_AT_low_pc, which is the
11065 base of the text section. */
11066 if (!have_multiple_function_sections)
11067 {
11068 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
11069 text_section_label,
11070 fmt, i * 2 * DWARF2_ADDR_SIZE);
11071 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
11072 text_section_label, NULL);
11073 }
11074
11075 /* Otherwise, the compilation unit base address is zero,
11076 which allows us to use absolute addresses, and not worry
11077 about whether the target supports cross-section
11078 arithmetic. */
11079 else
11080 {
11081 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11082 fmt, i * 2 * DWARF2_ADDR_SIZE);
11083 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
11084 }
11085
11086 fmt = NULL;
11087 }
11088
11089 /* Negative block_num stands for an index into ranges_by_label. */
11090 else if (block_num < 0)
11091 {
11092 int lab_idx = - block_num - 1;
11093
11094 if (!have_multiple_function_sections)
11095 {
11096 gcc_unreachable ();
11097#if 0
11098 /* If we ever use add_ranges_by_labels () for a single
11099 function section, all we have to do is to take out
11100 the #if 0 above. */
11101 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
11102 (*ranges_by_label)[lab_idx].begin,
11103 text_section_label,
11104 fmt, i * 2 * DWARF2_ADDR_SIZE);
11105 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
11106 (*ranges_by_label)[lab_idx].end,
11107 text_section_label, NULL);
11108#endif
11109 }
11110 else
11111 {
11112 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11113 (*ranges_by_label)[lab_idx].begin,
11114 fmt, i * 2 * DWARF2_ADDR_SIZE);
11115 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11116 (*ranges_by_label)[lab_idx].end,
11117 NULL);
11118 }
11119 }
11120 else
11121 {
11122 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11123 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11124 fmt = start_fmt;
11125 }
11126 }
11127}
11128
11129/* Non-zero if .debug_line_str should be used for .debug_line section
11130 strings or strings that are likely shareable with those. */
11131#define DWARF5_USE_DEBUG_LINE_STR \
11132 (!DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET \
11133 && (DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) != 0 \
11134 /* FIXME: there is no .debug_line_str.dwo section, \
11135 for -gsplit-dwarf we should use DW_FORM_strx instead. */ \
11136 && !dwarf_split_debug_info)
11137
11138/* Assign .debug_rnglists indexes. */
11139
11140static void
11141index_rnglists (void)
11142{
11143 unsigned i;
11144 dw_ranges *r;
11145
11146 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11147 if (r->label)
11148 r->idx = rnglist_idx++;
11149}
11150
11151/* Emit .debug_rnglists section. */
11152
11153static void
11154output_rnglists (void)
11155{
11156 unsigned i;
11157 dw_ranges *r;
11158 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
11159 char l2[MAX_ARTIFICIAL_LABEL_BYTES];
11160 char basebuf[MAX_ARTIFICIAL_LABEL_BYTES];
11161
11162 switch_to_section (debug_ranges_section);
11163 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
11164 ASM_GENERATE_INTERNAL_LABEL (l1, DEBUG_RANGES_SECTION_LABEL, 2);
11165 ASM_GENERATE_INTERNAL_LABEL (l2, DEBUG_RANGES_SECTION_LABEL, 3);
11166 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11167 dw2_asm_output_data (4, 0xffffffff,
11168 "Initial length escape value indicating "
11169 "64-bit DWARF extension");
11170 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
11171 "Length of Range Lists");
11172 ASM_OUTPUT_LABEL (asm_out_file, l1);
11173 dw2_asm_output_data (2, dwarf_version, "DWARF Version");
11174 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
11175 dw2_asm_output_data (1, 0, "Segment Size");
11176 /* Emit the offset table only for -gsplit-dwarf. If we don't care
11177 about relocation sizes and primarily care about the size of .debug*
11178 sections in linked shared libraries and executables, then
11179 the offset table plus corresponding DW_FORM_rnglistx uleb128 indexes
11180 into it are usually larger than just DW_FORM_sec_offset offsets
11181 into the .debug_rnglists section. */
11182 dw2_asm_output_data (4, dwarf_split_debug_info ? rnglist_idx : 0,
11183 "Offset Entry Count");
11184 if (dwarf_split_debug_info)
11185 {
11186 ASM_OUTPUT_LABEL (asm_out_file, ranges_base_label);
11187 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11188 if (r->label)
11189 dw2_asm_output_delta (DWARF_OFFSET_SIZE, r->label,
11190 ranges_base_label, NULL);
11191 }
11192
11193 const char *lab = "";
11194 unsigned int len = vec_safe_length (ranges_table);
11195 const char *base = NULL;
11196 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11197 {
11198 int block_num = r->num;
11199
11200 if (r->label)
11201 {
11202 ASM_OUTPUT_LABEL (asm_out_file, r->label);
11203 lab = r->label;
11204 }
11205 if (HAVE_AS_LEB128 && (r->label || r->maybe_new_sec))
11206 base = NULL;
11207 if (block_num > 0)
11208 {
11209 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
11210 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
11211
11212 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
11213 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
11214
11215 if (HAVE_AS_LEB128)
11216 {
11217 /* If all code is in the text section, then the compilation
11218 unit base address defaults to DW_AT_low_pc, which is the
11219 base of the text section. */
11220 if (!have_multiple_function_sections)
11221 {
11222 dw2_asm_output_data (1, DW_RLE_offset_pair,
11223 "DW_RLE_offset_pair (%s)", lab);
11224 dw2_asm_output_delta_uleb128 (blabel, text_section_label,
11225 "Range begin address (%s)", lab);
11226 dw2_asm_output_delta_uleb128 (elabel, text_section_label,
11227 "Range end address (%s)", lab);
11228 continue;
11229 }
11230 if (base == NULL)
11231 {
11232 dw_ranges *r2 = NULL;
11233 if (i < len - 1)
11234 r2 = &(*ranges_table)[i + 1];
11235 if (r2
11236 && r2->num != 0
11237 && r2->label == NULL
11238 && !r2->maybe_new_sec)
11239 {
11240 dw2_asm_output_data (1, DW_RLE_base_address,
11241 "DW_RLE_base_address (%s)", lab);
11242 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11243 "Base address (%s)", lab);
11244 strcpy (basebuf, blabel);
11245 base = basebuf;
11246 }
11247 }
11248 if (base)
11249 {
11250 dw2_asm_output_data (1, DW_RLE_offset_pair,
11251 "DW_RLE_offset_pair (%s)", lab);
11252 dw2_asm_output_delta_uleb128 (blabel, base,
11253 "Range begin address (%s)", lab);
11254 dw2_asm_output_delta_uleb128 (elabel, base,
11255 "Range end address (%s)", lab);
11256 continue;
11257 }
11258 dw2_asm_output_data (1, DW_RLE_start_length,
11259 "DW_RLE_start_length (%s)", lab);
11260 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11261 "Range begin address (%s)", lab);
11262 dw2_asm_output_delta_uleb128 (elabel, blabel,
11263 "Range length (%s)", lab);
11264 }
11265 else
11266 {
11267 dw2_asm_output_data (1, DW_RLE_start_end,
11268 "DW_RLE_start_end (%s)", lab);
11269 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11270 "Range begin address (%s)", lab);
11271 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel,
11272 "Range end address (%s)", lab);
11273 }
11274 }
11275
11276 /* Negative block_num stands for an index into ranges_by_label. */
11277 else if (block_num < 0)
11278 {
11279 int lab_idx = - block_num - 1;
11280 const char *blabel = (*ranges_by_label)[lab_idx].begin;
11281 const char *elabel = (*ranges_by_label)[lab_idx].end;
11282
11283 if (!have_multiple_function_sections)
11284 gcc_unreachable ();
11285 if (HAVE_AS_LEB128)
11286 {
11287 dw2_asm_output_data (1, DW_RLE_start_length,
11288 "DW_RLE_start_length (%s)", lab);
11289 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11290 "Range begin address (%s)", lab);
11291 dw2_asm_output_delta_uleb128 (elabel, blabel,
11292 "Range length (%s)", lab);
11293 }
11294 else
11295 {
11296 dw2_asm_output_data (1, DW_RLE_start_end,
11297 "DW_RLE_start_end (%s)", lab);
11298 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11299 "Range begin address (%s)", lab);
11300 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel,
11301 "Range end address (%s)", lab);
11302 }
11303 }
11304 else
11305 dw2_asm_output_data (1, DW_RLE_end_of_list,
11306 "DW_RLE_end_of_list (%s)", lab);
11307 }
11308 ASM_OUTPUT_LABEL (asm_out_file, l2);
11309}
11310
11311/* Data structure containing information about input files. */
11312struct file_info
11313{
11314 const char *path; /* Complete file name. */
11315 const char *fname; /* File name part. */
11316 int length; /* Length of entire string. */
11317 struct dwarf_file_data * file_idx; /* Index in input file table. */
11318 int dir_idx; /* Index in directory table. */
11319};
11320
11321/* Data structure containing information about directories with source
11322 files. */
11323struct dir_info
11324{
11325 const char *path; /* Path including directory name. */
11326 int length; /* Path length. */
11327 int prefix; /* Index of directory entry which is a prefix. */
11328 int count; /* Number of files in this directory. */
11329 int dir_idx; /* Index of directory used as base. */
11330};
11331
11332/* Callback function for file_info comparison. We sort by looking at
11333 the directories in the path. */
11334
11335static int
11336file_info_cmp (const void *p1, const void *p2)
11337{
11338 const struct file_info *const s1 = (const struct file_info *) p1;
11339 const struct file_info *const s2 = (const struct file_info *) p2;
11340 const unsigned char *cp1;
11341 const unsigned char *cp2;
11342
11343 /* Take care of file names without directories. We need to make sure that
11344 we return consistent values to qsort since some will get confused if
11345 we return the same value when identical operands are passed in opposite
11346 orders. So if neither has a directory, return 0 and otherwise return
11347 1 or -1 depending on which one has the directory. */
11348 if ((s1->path == s1->fname || s2->path == s2->fname))
11349 return (s2->path == s2->fname) - (s1->path == s1->fname);
11350
11351 cp1 = (const unsigned char *) s1->path;
11352 cp2 = (const unsigned char *) s2->path;
11353
11354 while (1)
11355 {
11356 ++cp1;
11357 ++cp2;
11358 /* Reached the end of the first path? If so, handle like above. */
11359 if ((cp1 == (const unsigned char *) s1->fname)
11360 || (cp2 == (const unsigned char *) s2->fname))
11361 return ((cp2 == (const unsigned char *) s2->fname)
11362 - (cp1 == (const unsigned char *) s1->fname));
11363
11364 /* Character of current path component the same? */
11365 else if (*cp1 != *cp2)
11366 return *cp1 - *cp2;
11367 }
11368}
11369
11370struct file_name_acquire_data
11371{
11372 struct file_info *files;
11373 int used_files;
11374 int max_files;
11375};
11376
11377/* Traversal function for the hash table. */
11378
11379int
11380file_name_acquire (dwarf_file_data **slot, file_name_acquire_data *fnad)
11381{
11382 struct dwarf_file_data *d = *slot;
11383 struct file_info *fi;
11384 const char *f;
11385
11386 gcc_assert (fnad->max_files >= d->emitted_number);
11387
11388 if (! d->emitted_number)
11389 return 1;
11390
11391 gcc_assert (fnad->max_files != fnad->used_files);
11392
11393 fi = fnad->files + fnad->used_files++;
11394
11395 /* Skip all leading "./". */
11396 f = d->filename;
11397 while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
11398 f += 2;
11399
11400 /* Create a new array entry. */
11401 fi->path = f;
11402 fi->length = strlen (f);
11403 fi->file_idx = d;
11404
11405 /* Search for the file name part. */
11406 f = strrchr (f, DIR_SEPARATOR);
11407#if defined (DIR_SEPARATOR_2)
11408 {
11409 char *g = strrchr (fi->path, DIR_SEPARATOR_2);
11410
11411 if (g != NULL)
11412 {
11413 if (f == NULL || f < g)
11414 f = g;
11415 }
11416 }
11417#endif
11418
11419 fi->fname = f == NULL ? fi->path : f + 1;
11420 return 1;
11421}
11422
11423/* Helper function for output_file_names. Emit a FORM encoded
11424 string STR, with assembly comment start ENTRY_KIND and
11425 index IDX */
11426
11427static void
11428output_line_string (enum dwarf_form form, const char *str,
11429 const char *entry_kind, unsigned int idx)
11430{
11431 switch (form)
11432 {
11433 case DW_FORM_string:
11434 dw2_asm_output_nstring (str, -1, "%s: %#x", entry_kind, idx);
11435 break;
11436 case DW_FORM_line_strp:
11437 if (!debug_line_str_hash)
11438 debug_line_str_hash
11439 = hash_table<indirect_string_hasher>::create_ggc (10);
11440
11441 struct indirect_string_node *node;
11442 node = find_AT_string_in_table (str, debug_line_str_hash);
11443 set_indirect_string (node);
11444 node->form = form;
11445 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
11446 debug_line_str_section, "%s: %#x: \"%s\"",
11447 entry_kind, 0, node->str);
11448 break;
11449 default:
11450 gcc_unreachable ();
11451 }
11452}
11453
11454/* Output the directory table and the file name table. We try to minimize
11455 the total amount of memory needed. A heuristic is used to avoid large
11456 slowdowns with many input files. */
11457
11458static void
11459output_file_names (void)
11460{
11461 struct file_name_acquire_data fnad;
11462 int numfiles;
11463 struct file_info *files;
11464 struct dir_info *dirs;
11465 int *saved;
11466 int *savehere;
11467 int *backmap;
11468 int ndirs;
11469 int idx_offset;
11470 int i;
11471
11472 if (!last_emitted_file)
11473 {
11474 if (dwarf_version >= 5)
11475 {
11476 dw2_asm_output_data (1, 0, "Directory entry format count");
11477 dw2_asm_output_data_uleb128 (0, "Directories count");
11478 dw2_asm_output_data (1, 0, "File name entry format count");
11479 dw2_asm_output_data_uleb128 (0, "File names count");
11480 }
11481 else
11482 {
11483 dw2_asm_output_data (1, 0, "End directory table");
11484 dw2_asm_output_data (1, 0, "End file name table");
11485 }
11486 return;
11487 }
11488
11489 numfiles = last_emitted_file->emitted_number;
11490
11491 /* Allocate the various arrays we need. */
11492 files = XALLOCAVEC (struct file_info, numfiles);
11493 dirs = XALLOCAVEC (struct dir_info, numfiles);
11494
11495 fnad.files = files;
11496 fnad.used_files = 0;
11497 fnad.max_files = numfiles;
11498 file_table->traverse<file_name_acquire_data *, file_name_acquire> (&fnad);
11499 gcc_assert (fnad.used_files == fnad.max_files);
11500
11501 qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
11502
11503 /* Find all the different directories used. */
11504 dirs[0].path = files[0].path;
11505 dirs[0].length = files[0].fname - files[0].path;
11506 dirs[0].prefix = -1;
11507 dirs[0].count = 1;
11508 dirs[0].dir_idx = 0;
11509 files[0].dir_idx = 0;
11510 ndirs = 1;
11511
11512 for (i = 1; i < numfiles; i++)
11513 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
11514 && memcmp (dirs[ndirs - 1].path, files[i].path,
11515 dirs[ndirs - 1].length) == 0)
11516 {
11517 /* Same directory as last entry. */
11518 files[i].dir_idx = ndirs - 1;
11519 ++dirs[ndirs - 1].count;
11520 }
11521 else
11522 {
11523 int j;
11524
11525 /* This is a new directory. */
11526 dirs[ndirs].path = files[i].path;
11527 dirs[ndirs].length = files[i].fname - files[i].path;
11528 dirs[ndirs].count = 1;
11529 dirs[ndirs].dir_idx = ndirs;
11530 files[i].dir_idx = ndirs;
11531
11532 /* Search for a prefix. */
11533 dirs[ndirs].prefix = -1;
11534 for (j = 0; j < ndirs; j++)
11535 if (dirs[j].length < dirs[ndirs].length
11536 && dirs[j].length > 1
11537 && (dirs[ndirs].prefix == -1
11538 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
11539 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
11540 dirs[ndirs].prefix = j;
11541
11542 ++ndirs;
11543 }
11544
11545 /* Now to the actual work. We have to find a subset of the directories which
11546 allow expressing the file name using references to the directory table
11547 with the least amount of characters. We do not do an exhaustive search
11548 where we would have to check out every combination of every single
11549 possible prefix. Instead we use a heuristic which provides nearly optimal
11550 results in most cases and never is much off. */
11551 saved = XALLOCAVEC (int, ndirs);
11552 savehere = XALLOCAVEC (int, ndirs);
11553
11554 memset (saved, '\0', ndirs * sizeof (saved[0]));
11555 for (i = 0; i < ndirs; i++)
11556 {
11557 int j;
11558 int total;
11559
11560 /* We can always save some space for the current directory. But this
11561 does not mean it will be enough to justify adding the directory. */
11562 savehere[i] = dirs[i].length;
11563 total = (savehere[i] - saved[i]) * dirs[i].count;
11564
11565 for (j = i + 1; j < ndirs; j++)
11566 {
11567 savehere[j] = 0;
11568 if (saved[j] < dirs[i].length)
11569 {
11570 /* Determine whether the dirs[i] path is a prefix of the
11571 dirs[j] path. */
11572 int k;
11573
11574 k = dirs[j].prefix;
11575 while (k != -1 && k != (int) i)
11576 k = dirs[k].prefix;
11577
11578 if (k == (int) i)
11579 {
11580 /* Yes it is. We can possibly save some memory by
11581 writing the filenames in dirs[j] relative to
11582 dirs[i]. */
11583 savehere[j] = dirs[i].length;
11584 total += (savehere[j] - saved[j]) * dirs[j].count;
11585 }
11586 }
11587 }
11588
11589 /* Check whether we can save enough to justify adding the dirs[i]
11590 directory. */
11591 if (total > dirs[i].length + 1)
11592 {
11593 /* It's worthwhile adding. */
11594 for (j = i; j < ndirs; j++)
11595 if (savehere[j] > 0)
11596 {
11597 /* Remember how much we saved for this directory so far. */
11598 saved[j] = savehere[j];
11599
11600 /* Remember the prefix directory. */
11601 dirs[j].dir_idx = i;
11602 }
11603 }
11604 }
11605
11606 /* Emit the directory name table. */
11607 idx_offset = dirs[0].length > 0 ? 1 : 0;
11608 enum dwarf_form str_form = DW_FORM_string;
11609 enum dwarf_form idx_form = DW_FORM_udata;
11610 if (dwarf_version >= 5)
11611 {
11612 const char *comp_dir = comp_dir_string ();
11613 if (comp_dir == NULL)
11614 comp_dir = "";
11615 dw2_asm_output_data (1, 1, "Directory entry format count");
11616 if (DWARF5_USE_DEBUG_LINE_STR)
11617 str_form = DW_FORM_line_strp;
11618 dw2_asm_output_data_uleb128 (DW_LNCT_path, "DW_LNCT_path");
11619 dw2_asm_output_data_uleb128 (str_form, "%s",
11620 get_DW_FORM_name (str_form));
11621 dw2_asm_output_data_uleb128 (ndirs + idx_offset, "Directories count");
11622 if (str_form == DW_FORM_string)
11623 {
11624 dw2_asm_output_nstring (comp_dir, -1, "Directory Entry: %#x", 0);
11625 for (i = 1 - idx_offset; i < ndirs; i++)
11626 dw2_asm_output_nstring (dirs[i].path,
11627 dirs[i].length
11628 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
11629 "Directory Entry: %#x", i + idx_offset);
11630 }
11631 else
11632 {
11633 output_line_string (str_form, comp_dir, "Directory Entry", 0);
11634 for (i = 1 - idx_offset; i < ndirs; i++)
11635 {
11636 const char *str
11637 = ggc_alloc_string (dirs[i].path,
11638 dirs[i].length
11639 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR);
11640 output_line_string (str_form, str, "Directory Entry",
11641 (unsigned) i + idx_offset);
11642 }
11643 }
11644 }
11645 else
11646 {
11647 for (i = 1 - idx_offset; i < ndirs; i++)
11648 dw2_asm_output_nstring (dirs[i].path,
11649 dirs[i].length
11650 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
11651 "Directory Entry: %#x", i + idx_offset);
11652
11653 dw2_asm_output_data (1, 0, "End directory table");
11654 }
11655
11656 /* We have to emit them in the order of emitted_number since that's
11657 used in the debug info generation. To do this efficiently we
11658 generate a back-mapping of the indices first. */
11659 backmap = XALLOCAVEC (int, numfiles);
11660 for (i = 0; i < numfiles; i++)
11661 backmap[files[i].file_idx->emitted_number - 1] = i;
11662
11663 if (dwarf_version >= 5)
11664 {
11665 const char *filename0 = get_AT_string (comp_unit_die (), DW_AT_name);
11666 if (filename0 == NULL)
11667 filename0 = "";
11668 /* DW_LNCT_directory_index can use DW_FORM_udata, DW_FORM_data1 and
11669 DW_FORM_data2. Choose one based on the number of directories
11670 and how much space would they occupy in each encoding.
11671 If we have at most 256 directories, all indexes fit into
11672 a single byte, so DW_FORM_data1 is most compact (if there
11673 are at most 128 directories, DW_FORM_udata would be as
11674 compact as that, but not shorter and slower to decode). */
11675 if (ndirs + idx_offset <= 256)
11676 idx_form = DW_FORM_data1;
11677 /* If there are more than 65536 directories, we have to use
11678 DW_FORM_udata, DW_FORM_data2 can't refer to them.
11679 Otherwise, compute what space would occupy if all the indexes
11680 used DW_FORM_udata - sum - and compare that to how large would
11681 be DW_FORM_data2 encoding, and pick the more efficient one. */
11682 else if (ndirs + idx_offset <= 65536)
11683 {
11684 unsigned HOST_WIDE_INT sum = 1;
11685 for (i = 0; i < numfiles; i++)
11686 {
11687 int file_idx = backmap[i];
11688 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
11689 sum += size_of_uleb128 (dir_idx);
11690 }
11691 if (sum >= HOST_WIDE_INT_UC (2) * (numfiles + 1))
11692 idx_form = DW_FORM_data2;
11693 }
11694#ifdef VMS_DEBUGGING_INFO
11695 dw2_asm_output_data (1, 4, "File name entry format count");
11696#else
11697 dw2_asm_output_data (1, 2, "File name entry format count");
11698#endif
11699 dw2_asm_output_data_uleb128 (DW_LNCT_path, "DW_LNCT_path");
11700 dw2_asm_output_data_uleb128 (str_form, "%s",
11701 get_DW_FORM_name (str_form));
11702 dw2_asm_output_data_uleb128 (DW_LNCT_directory_index,
11703 "DW_LNCT_directory_index");
11704 dw2_asm_output_data_uleb128 (idx_form, "%s",
11705 get_DW_FORM_name (idx_form));
11706#ifdef VMS_DEBUGGING_INFO
11707 dw2_asm_output_data_uleb128 (DW_LNCT_timestamp, "DW_LNCT_timestamp");
11708 dw2_asm_output_data_uleb128 (DW_FORM_udata, "DW_FORM_udata");
11709 dw2_asm_output_data_uleb128 (DW_LNCT_size, "DW_LNCT_size");
11710 dw2_asm_output_data_uleb128 (DW_FORM_udata, "DW_FORM_udata");
11711#endif
11712 dw2_asm_output_data_uleb128 (numfiles + 1, "File names count");
11713
11714 output_line_string (str_form, filename0, "File Entry", 0);
11715
11716 /* Include directory index. */
11717 if (dwarf_version >= 5 && idx_form != DW_FORM_udata)
11718 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
11719 0, NULL);
11720 else
11721 dw2_asm_output_data_uleb128 (0, NULL);
11722
11723#ifdef VMS_DEBUGGING_INFO
11724 dw2_asm_output_data_uleb128 (0, NULL);
11725 dw2_asm_output_data_uleb128 (0, NULL);
11726#endif
11727 }
11728
11729 /* Now write all the file names. */
11730 for (i = 0; i < numfiles; i++)
11731 {
11732 int file_idx = backmap[i];
11733 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
11734
11735#ifdef VMS_DEBUGGING_INFO
11736#define MAX_VMS_VERSION_LEN 6 /* ";32768" */
11737
11738 /* Setting these fields can lead to debugger miscomparisons,
11739 but VMS Debug requires them to be set correctly. */
11740
11741 int ver;
11742 long long cdt;
11743 long siz;
11744 int maxfilelen = (strlen (files[file_idx].path)
11745 + dirs[dir_idx].length
11746 + MAX_VMS_VERSION_LEN + 1);
11747 char *filebuf = XALLOCAVEC (char, maxfilelen);
11748
11749 vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
11750 snprintf (filebuf, maxfilelen, "%s;%d",
11751 files[file_idx].path + dirs[dir_idx].length, ver);
11752
11753 output_line_string (str_form, filebuf, "File Entry", (unsigned) i + 1);
11754
11755 /* Include directory index. */
11756 if (dwarf_version >= 5 && idx_form != DW_FORM_udata)
11757 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
11758 dir_idx + idx_offset, NULL);
11759 else
11760 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
11761
11762 /* Modification time. */
11763 dw2_asm_output_data_uleb128 ((vms_file_stats_name (files[file_idx].path,
11764 &cdt, 0, 0, 0) == 0)
11765 ? cdt : 0, NULL);
11766
11767 /* File length in bytes. */
11768 dw2_asm_output_data_uleb128 ((vms_file_stats_name (files[file_idx].path,
11769 0, &siz, 0, 0) == 0)
11770 ? siz : 0, NULL);
11771#else
11772 output_line_string (str_form,
11773 files[file_idx].path + dirs[dir_idx].length,
11774 "File Entry", (unsigned) i + 1);
11775
11776 /* Include directory index. */
11777 if (dwarf_version >= 5 && idx_form != DW_FORM_udata)
11778 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
11779 dir_idx + idx_offset, NULL);
11780 else
11781 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
11782
11783 if (dwarf_version >= 5)
11784 continue;
11785
11786 /* Modification time. */
11787 dw2_asm_output_data_uleb128 (0, NULL);
11788
11789 /* File length in bytes. */
11790 dw2_asm_output_data_uleb128 (0, NULL);
11791#endif /* VMS_DEBUGGING_INFO */
11792 }
11793
11794 if (dwarf_version < 5)
11795 dw2_asm_output_data (1, 0, "End file name table");
11796}
11797
11798
11799/* Output one line number table into the .debug_line section. */
11800
11801static void
11802output_one_line_info_table (dw_line_info_table *table)
11803{
11804 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
11805 unsigned int current_line = 1;
11806 bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
11807 dw_line_info_entry *ent;
11808 size_t i;
11809
11810 FOR_EACH_VEC_SAFE_ELT (table->entries, i, ent)
11811 {
11812 switch (ent->opcode)
11813 {
11814 case LI_set_address:
11815 /* ??? Unfortunately, we have little choice here currently, and
11816 must always use the most general form. GCC does not know the
11817 address delta itself, so we can't use DW_LNS_advance_pc. Many
11818 ports do have length attributes which will give an upper bound
11819 on the address range. We could perhaps use length attributes
11820 to determine when it is safe to use DW_LNS_fixed_advance_pc. */
11821 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
11822
11823 /* This can handle any delta. This takes
11824 4+DWARF2_ADDR_SIZE bytes. */
11825 dw2_asm_output_data (1, 0, "set address %s", line_label);
11826 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
11827 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
11828 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
11829 break;
11830
11831 case LI_set_line:
11832 if (ent->val == current_line)
11833 {
11834 /* We still need to start a new row, so output a copy insn. */
11835 dw2_asm_output_data (1, DW_LNS_copy,
11836 "copy line %u", current_line);
11837 }
11838 else
11839 {
11840 int line_offset = ent->val - current_line;
11841 int line_delta = line_offset - DWARF_LINE_BASE;
11842
11843 current_line = ent->val;
11844 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
11845 {
11846 /* This can handle deltas from -10 to 234, using the current
11847 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
11848 This takes 1 byte. */
11849 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
11850 "line %u", current_line);
11851 }
11852 else
11853 {
11854 /* This can handle any delta. This takes at least 4 bytes,
11855 depending on the value being encoded. */
11856 dw2_asm_output_data (1, DW_LNS_advance_line,
11857 "advance to line %u", current_line);
11858 dw2_asm_output_data_sleb128 (line_offset, NULL);
11859 dw2_asm_output_data (1, DW_LNS_copy, NULL);
11860 }
11861 }
11862 break;
11863
11864 case LI_set_file:
11865 dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
11866 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
11867 break;
11868
11869 case LI_set_column:
11870 dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
11871 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
11872 break;
11873
11874 case LI_negate_stmt:
11875 current_is_stmt = !current_is_stmt;
11876 dw2_asm_output_data (1, DW_LNS_negate_stmt,
11877 "is_stmt %d", current_is_stmt);
11878 break;
11879
11880 case LI_set_prologue_end:
11881 dw2_asm_output_data (1, DW_LNS_set_prologue_end,
11882 "set prologue end");
11883 break;
11884
11885 case LI_set_epilogue_begin:
11886 dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
11887 "set epilogue begin");
11888 break;
11889
11890 case LI_set_discriminator:
11891 dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
11892 dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
11893 dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
11894 dw2_asm_output_data_uleb128 (ent->val, NULL);
11895 break;
11896 }
11897 }
11898
11899 /* Emit debug info for the address of the end of the table. */
11900 dw2_asm_output_data (1, 0, "set address %s", table->end_label);
11901 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
11902 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
11903 dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
11904
11905 dw2_asm_output_data (1, 0, "end sequence");
11906 dw2_asm_output_data_uleb128 (1, NULL);
11907 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
11908}
11909
11910/* Output the source line number correspondence information. This
11911 information goes into the .debug_line section. */
11912
11913static void
11914output_line_info (bool prologue_only)
11915{
11916 static unsigned int generation;
11917 char l1[MAX_ARTIFICIAL_LABEL_BYTES], l2[MAX_ARTIFICIAL_LABEL_BYTES];
11918 char p1[MAX_ARTIFICIAL_LABEL_BYTES], p2[MAX_ARTIFICIAL_LABEL_BYTES];
11919 bool saw_one = false;
11920 int opc;
11921
11922 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, generation);
11923 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, generation);
11924 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, generation);
11925 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, generation++);
11926
11927 if (!XCOFF_DEBUGGING_INFO)
11928 {
11929 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11930 dw2_asm_output_data (4, 0xffffffff,
11931 "Initial length escape value indicating 64-bit DWARF extension");
11932 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
11933 "Length of Source Line Info");
11934 }
11935
11936 ASM_OUTPUT_LABEL (asm_out_file, l1);
11937
11938 dw2_asm_output_data (2, dwarf_version, "DWARF Version");
11939 if (dwarf_version >= 5)
11940 {
11941 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
11942 dw2_asm_output_data (1, 0, "Segment Size");
11943 }
11944 dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
11945 ASM_OUTPUT_LABEL (asm_out_file, p1);
11946
11947 /* Define the architecture-dependent minimum instruction length (in bytes).
11948 In this implementation of DWARF, this field is used for information
11949 purposes only. Since GCC generates assembly language, we have no
11950 a priori knowledge of how many instruction bytes are generated for each
11951 source line, and therefore can use only the DW_LNE_set_address and
11952 DW_LNS_fixed_advance_pc line information commands. Accordingly, we fix
11953 this as '1', which is "correct enough" for all architectures,
11954 and don't let the target override. */
11955 dw2_asm_output_data (1, 1, "Minimum Instruction Length");
11956
11957 if (dwarf_version >= 4)
11958 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
11959 "Maximum Operations Per Instruction");
11960 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
11961 "Default is_stmt_start flag");
11962 dw2_asm_output_data (1, DWARF_LINE_BASE,
11963 "Line Base Value (Special Opcodes)");
11964 dw2_asm_output_data (1, DWARF_LINE_RANGE,
11965 "Line Range Value (Special Opcodes)");
11966 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
11967 "Special Opcode Base");
11968
11969 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
11970 {
11971 int n_op_args;
11972 switch (opc)
11973 {
11974 case DW_LNS_advance_pc:
11975 case DW_LNS_advance_line:
11976 case DW_LNS_set_file:
11977 case DW_LNS_set_column:
11978 case DW_LNS_fixed_advance_pc:
11979 case DW_LNS_set_isa:
11980 n_op_args = 1;
11981 break;
11982 default:
11983 n_op_args = 0;
11984 break;
11985 }
11986
11987 dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
11988 opc, n_op_args);
11989 }
11990
11991 /* Write out the information about the files we use. */
11992 output_file_names ();
11993 ASM_OUTPUT_LABEL (asm_out_file, p2);
11994 if (prologue_only)
11995 {
11996 /* Output the marker for the end of the line number info. */
11997 ASM_OUTPUT_LABEL (asm_out_file, l2);
11998 return;
11999 }
12000
12001 if (separate_line_info)
12002 {
12003 dw_line_info_table *table;
12004 size_t i;
12005
12006 FOR_EACH_VEC_ELT (*separate_line_info, i, table)
12007 if (table->in_use)
12008 {
12009 output_one_line_info_table (table);
12010 saw_one = true;
12011 }
12012 }
12013 if (cold_text_section_line_info && cold_text_section_line_info->in_use)
12014 {
12015 output_one_line_info_table (cold_text_section_line_info);
12016 saw_one = true;
12017 }
12018
12019 /* ??? Some Darwin linkers crash on a .debug_line section with no
12020 sequences. Further, merely a DW_LNE_end_sequence entry is not
12021 sufficient -- the address column must also be initialized.
12022 Make sure to output at least one set_address/end_sequence pair,
12023 choosing .text since that section is always present. */
12024 if (text_section_line_info->in_use || !saw_one)
12025 output_one_line_info_table (text_section_line_info);
12026
12027 /* Output the marker for the end of the line number info. */
12028 ASM_OUTPUT_LABEL (asm_out_file, l2);
12029}
12030
12031/* Return true if DW_AT_endianity should be emitted according to REVERSE. */
12032
12033static inline bool
12034need_endianity_attribute_p (bool reverse)
12035{
12036 return reverse && (dwarf_version >= 3 || !dwarf_strict);
12037}
12038
12039/* Given a pointer to a tree node for some base type, return a pointer to
12040 a DIE that describes the given type. REVERSE is true if the type is
12041 to be interpreted in the reverse storage order wrt the target order.
12042
12043 This routine must only be called for GCC type nodes that correspond to
12044 Dwarf base (fundamental) types. */
12045
12046static dw_die_ref
12047base_type_die (tree type, bool reverse)
12048{
12049 dw_die_ref base_type_result;
12050 enum dwarf_type encoding;
12051 bool fpt_used = false;
12052 struct fixed_point_type_info fpt_info;
12053 tree type_bias = NULL_TREE;
12054
12055 if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
12056 return 0;
12057
12058 /* If this is a subtype that should not be emitted as a subrange type,
12059 use the base type. See subrange_type_for_debug_p. */
12060 if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
12061 type = TREE_TYPE (type);
12062
12063 switch (TREE_CODE (type))
12064 {
12065 case INTEGER_TYPE:
12066 if ((dwarf_version >= 4 || !dwarf_strict)
12067 && TYPE_NAME (type)
12068 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
12069 && DECL_IS_BUILTIN (TYPE_NAME (type))
12070 && DECL_NAME (TYPE_NAME (type)))
12071 {
12072 const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
12073 if (strcmp (name, "char16_t") == 0
12074 || strcmp (name, "char32_t") == 0)
12075 {
12076 encoding = DW_ATE_UTF;
12077 break;
12078 }
12079 }
12080 if ((dwarf_version >= 3 || !dwarf_strict)
12081 && lang_hooks.types.get_fixed_point_type_info)
12082 {
12083 memset (&fpt_info, 0, sizeof (fpt_info));
12084 if (lang_hooks.types.get_fixed_point_type_info (type, &fpt_info))
12085 {
12086 fpt_used = true;
12087 encoding = ((TYPE_UNSIGNED (type))
12088 ? DW_ATE_unsigned_fixed
12089 : DW_ATE_signed_fixed);
12090 break;
12091 }
12092 }
12093 if (TYPE_STRING_FLAG (type))
12094 {
12095 if (TYPE_UNSIGNED (type))
12096 encoding = DW_ATE_unsigned_char;
12097 else
12098 encoding = DW_ATE_signed_char;
12099 }
12100 else if (TYPE_UNSIGNED (type))
12101 encoding = DW_ATE_unsigned;
12102 else
12103 encoding = DW_ATE_signed;
12104
12105 if (!dwarf_strict
12106 && lang_hooks.types.get_type_bias)
12107 type_bias = lang_hooks.types.get_type_bias (type);
12108 break;
12109
12110 case REAL_TYPE:
12111 if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
12112 {
12113 if (dwarf_version >= 3 || !dwarf_strict)
12114 encoding = DW_ATE_decimal_float;
12115 else
12116 encoding = DW_ATE_lo_user;
12117 }
12118 else
12119 encoding = DW_ATE_float;
12120 break;
12121
12122 case FIXED_POINT_TYPE:
12123 if (!(dwarf_version >= 3 || !dwarf_strict))
12124 encoding = DW_ATE_lo_user;
12125 else if (TYPE_UNSIGNED (type))
12126 encoding = DW_ATE_unsigned_fixed;
12127 else
12128 encoding = DW_ATE_signed_fixed;
12129 break;
12130
12131 /* Dwarf2 doesn't know anything about complex ints, so use
12132 a user defined type for it. */
12133 case COMPLEX_TYPE:
12134 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
12135 encoding = DW_ATE_complex_float;
12136 else
12137 encoding = DW_ATE_lo_user;
12138 break;
12139
12140 case BOOLEAN_TYPE:
12141 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
12142 encoding = DW_ATE_boolean;
12143 break;
12144
12145 default:
12146 /* No other TREE_CODEs are Dwarf fundamental types. */
12147 gcc_unreachable ();
12148 }
12149
12150 base_type_result = new_die (DW_TAG_base_type, comp_unit_die (), type);
12151
12152 add_AT_unsigned (base_type_result, DW_AT_byte_size,
12153 int_size_in_bytes (type));
12154 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
12155
12156 if (need_endianity_attribute_p (reverse))
12157 add_AT_unsigned (base_type_result, DW_AT_endianity,
12158 BYTES_BIG_ENDIAN ? DW_END_little : DW_END_big);
12159
12160 add_alignment_attribute (base_type_result, type);
12161
12162 if (fpt_used)
12163 {
12164 switch (fpt_info.scale_factor_kind)
12165 {
12166 case fixed_point_scale_factor_binary:
12167 add_AT_int (base_type_result, DW_AT_binary_scale,
12168 fpt_info.scale_factor.binary);
12169 break;
12170
12171 case fixed_point_scale_factor_decimal:
12172 add_AT_int (base_type_result, DW_AT_decimal_scale,
12173 fpt_info.scale_factor.decimal);
12174 break;
12175
12176 case fixed_point_scale_factor_arbitrary:
12177 /* Arbitrary scale factors cannot be described in standard DWARF,
12178 yet. */
12179 if (!dwarf_strict)
12180 {
12181 /* Describe the scale factor as a rational constant. */
12182 const dw_die_ref scale_factor
12183 = new_die (DW_TAG_constant, comp_unit_die (), type);
12184
12185 add_AT_unsigned (scale_factor, DW_AT_GNU_numerator,
12186 fpt_info.scale_factor.arbitrary.numerator);
12187 add_AT_int (scale_factor, DW_AT_GNU_denominator,
12188 fpt_info.scale_factor.arbitrary.denominator);
12189
12190 add_AT_die_ref (base_type_result, DW_AT_small, scale_factor);
12191 }
12192 break;
12193
12194 default:
12195 gcc_unreachable ();
12196 }
12197 }
12198
12199 if (type_bias)
12200 add_scalar_info (base_type_result, DW_AT_GNU_bias, type_bias,
12201 dw_scalar_form_constant
12202 | dw_scalar_form_exprloc
12203 | dw_scalar_form_reference,
12204 NULL);
12205
12206 add_pubtype (type, base_type_result);
12207
12208 return base_type_result;
12209}
12210
12211/* A C++ function with deduced return type can have a TEMPLATE_TYPE_PARM
12212 named 'auto' in its type: return true for it, false otherwise. */
12213
12214static inline bool
12215is_cxx_auto (tree type)
12216{
12217 if (is_cxx ())
12218 {
12219 tree name = TYPE_IDENTIFIER (type);
12220 if (name == get_identifier ("auto")
12221 || name == get_identifier ("decltype(auto)"))
12222 return true;
12223 }
12224 return false;
12225}
12226
12227/* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
12228 given input type is a Dwarf "fundamental" type. Otherwise return null. */
12229
12230static inline int
12231is_base_type (tree type)
12232{
12233 switch (TREE_CODE (type))
12234 {
12235 case ERROR_MARK:
12236 case VOID_TYPE:
12237 case INTEGER_TYPE:
12238 case REAL_TYPE:
12239 case FIXED_POINT_TYPE:
12240 case COMPLEX_TYPE:
12241 case BOOLEAN_TYPE:
12242 case POINTER_BOUNDS_TYPE:
12243 return 1;
12244
12245 case ARRAY_TYPE:
12246 case RECORD_TYPE:
12247 case UNION_TYPE:
12248 case QUAL_UNION_TYPE:
12249 case ENUMERAL_TYPE:
12250 case FUNCTION_TYPE:
12251 case METHOD_TYPE:
12252 case POINTER_TYPE:
12253 case REFERENCE_TYPE:
12254 case NULLPTR_TYPE:
12255 case OFFSET_TYPE:
12256 case LANG_TYPE:
12257 case VECTOR_TYPE:
12258 return 0;
12259
12260 default:
12261 if (is_cxx_auto (type))
12262 return 0;
12263 gcc_unreachable ();
12264 }
12265
12266 return 0;
12267}
12268
12269/* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
12270 node, return the size in bits for the type if it is a constant, or else
12271 return the alignment for the type if the type's size is not constant, or
12272 else return BITS_PER_WORD if the type actually turns out to be an
12273 ERROR_MARK node. */
12274
12275static inline unsigned HOST_WIDE_INT
12276simple_type_size_in_bits (const_tree type)
12277{
12278 if (TREE_CODE (type) == ERROR_MARK)
12279 return BITS_PER_WORD;
12280 else if (TYPE_SIZE (type) == NULL_TREE)
12281 return 0;
12282 else if (tree_fits_uhwi_p (TYPE_SIZE (type)))
12283 return tree_to_uhwi (TYPE_SIZE (type));
12284 else
12285 return TYPE_ALIGN (type);
12286}
12287
12288/* Similarly, but return an offset_int instead of UHWI. */
12289
12290static inline offset_int
12291offset_int_type_size_in_bits (const_tree type)
12292{
12293 if (TREE_CODE (type) == ERROR_MARK)
12294 return BITS_PER_WORD;
12295 else if (TYPE_SIZE (type) == NULL_TREE)
12296 return 0;
12297 else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
12298 return wi::to_offset (TYPE_SIZE (type));
12299 else
12300 return TYPE_ALIGN (type);
12301}
12302
12303/* Given a pointer to a tree node for a subrange type, return a pointer
12304 to a DIE that describes the given type. */
12305
12306static dw_die_ref
12307subrange_type_die (tree type, tree low, tree high, tree bias,
12308 dw_die_ref context_die)
12309{
12310 dw_die_ref subrange_die;
12311 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
12312
12313 if (context_die == NULL)
12314 context_die = comp_unit_die ();
12315
12316 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
12317
12318 if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
12319 {
12320 /* The size of the subrange type and its base type do not match,
12321 so we need to generate a size attribute for the subrange type. */
12322 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
12323 }
12324
12325 add_alignment_attribute (subrange_die, type);
12326
12327 if (low)
12328 add_bound_info (subrange_die, DW_AT_lower_bound, low, NULL);
12329 if (high)
12330 add_bound_info (subrange_die, DW_AT_upper_bound, high, NULL);
12331 if (bias && !dwarf_strict)
12332 add_scalar_info (subrange_die, DW_AT_GNU_bias, bias,
12333 dw_scalar_form_constant
12334 | dw_scalar_form_exprloc
12335 | dw_scalar_form_reference,
12336 NULL);
12337
12338 return subrange_die;
12339}
12340
12341/* Returns the (const and/or volatile) cv_qualifiers associated with
12342 the decl node. This will normally be augmented with the
12343 cv_qualifiers of the underlying type in add_type_attribute. */
12344
12345static int
12346decl_quals (const_tree decl)
12347{
12348 return ((TREE_READONLY (decl)
12349 /* The C++ front-end correctly marks reference-typed
12350 variables as readonly, but from a language (and debug
12351 info) standpoint they are not const-qualified. */
12352 && TREE_CODE (TREE_TYPE (decl)) != REFERENCE_TYPE
12353 ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED)
12354 | (TREE_THIS_VOLATILE (decl)
12355 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED));
12356}
12357
12358/* Determine the TYPE whose qualifiers match the largest strict subset
12359 of the given TYPE_QUALS, and return its qualifiers. Ignore all
12360 qualifiers outside QUAL_MASK. */
12361
12362static int
12363get_nearest_type_subqualifiers (tree type, int type_quals, int qual_mask)
12364{
12365 tree t;
12366 int best_rank = 0, best_qual = 0, max_rank;
12367
12368 type_quals &= qual_mask;
12369 max_rank = popcount_hwi (type_quals) - 1;
12370
12371 for (t = TYPE_MAIN_VARIANT (type); t && best_rank < max_rank;
12372 t = TYPE_NEXT_VARIANT (t))
12373 {
12374 int q = TYPE_QUALS (t) & qual_mask;
12375
12376 if ((q & type_quals) == q && q != type_quals
12377 && check_base_type (t, type))
12378 {
12379 int rank = popcount_hwi (q);
12380
12381 if (rank > best_rank)
12382 {
12383 best_rank = rank;
12384 best_qual = q;
12385 }
12386 }
12387 }
12388
12389 return best_qual;
12390}
12391
12392struct dwarf_qual_info_t { int q; enum dwarf_tag t; };
12393static const dwarf_qual_info_t dwarf_qual_info[] =
12394{
12395 { TYPE_QUAL_CONST, DW_TAG_const_type },
12396 { TYPE_QUAL_VOLATILE, DW_TAG_volatile_type },
12397 { TYPE_QUAL_RESTRICT, DW_TAG_restrict_type },
12398 { TYPE_QUAL_ATOMIC, DW_TAG_atomic_type }
12399};
12400static const unsigned int dwarf_qual_info_size
12401 = sizeof (dwarf_qual_info) / sizeof (dwarf_qual_info[0]);
12402
12403/* If DIE is a qualified DIE of some base DIE with the same parent,
12404 return the base DIE, otherwise return NULL. Set MASK to the
12405 qualifiers added compared to the returned DIE. */
12406
12407static dw_die_ref
12408qualified_die_p (dw_die_ref die, int *mask, unsigned int depth)
12409{
12410 unsigned int i;
12411 for (i = 0; i < dwarf_qual_info_size; i++)
12412 if (die->die_tag == dwarf_qual_info[i].t)
12413 break;
12414 if (i == dwarf_qual_info_size)
12415 return NULL;
12416 if (vec_safe_length (die->die_attr) != 1)
12417 return NULL;
12418 dw_die_ref type = get_AT_ref (die, DW_AT_type);
12419 if (type == NULL || type->die_parent != die->die_parent)
12420 return NULL;
12421 *mask |= dwarf_qual_info[i].q;
12422 if (depth)
12423 {
12424 dw_die_ref ret = qualified_die_p (type, mask, depth - 1);
12425 if (ret)
12426 return ret;
12427 }
12428 return type;
12429}
12430
12431/* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
12432 entry that chains the modifiers specified by CV_QUALS in front of the
12433 given type. REVERSE is true if the type is to be interpreted in the
12434 reverse storage order wrt the target order. */
12435
12436static dw_die_ref
12437modified_type_die (tree type, int cv_quals, bool reverse,
12438 dw_die_ref context_die)
12439{
12440 enum tree_code code = TREE_CODE (type);
12441 dw_die_ref mod_type_die;
12442 dw_die_ref sub_die = NULL;
12443 tree item_type = NULL;
12444 tree qualified_type;
12445 tree name, low, high;
12446 dw_die_ref mod_scope;
12447 /* Only these cv-qualifiers are currently handled. */
12448 const int cv_qual_mask = (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE
12449 | TYPE_QUAL_RESTRICT | TYPE_QUAL_ATOMIC);
12450
12451 if (code == ERROR_MARK)
12452 return NULL;
12453
12454 if (lang_hooks.types.get_debug_type)
12455 {
12456 tree debug_type = lang_hooks.types.get_debug_type (type);
12457
12458 if (debug_type != NULL_TREE && debug_type != type)
12459 return modified_type_die (debug_type, cv_quals, reverse, context_die);
12460 }
12461
12462 cv_quals &= cv_qual_mask;
12463
12464 /* Don't emit DW_TAG_restrict_type for DWARFv2, since it is a type
12465 tag modifier (and not an attribute) old consumers won't be able
12466 to handle it. */
12467 if (dwarf_version < 3)
12468 cv_quals &= ~TYPE_QUAL_RESTRICT;
12469
12470 /* Likewise for DW_TAG_atomic_type for DWARFv5. */
12471 if (dwarf_version < 5)
12472 cv_quals &= ~TYPE_QUAL_ATOMIC;
12473
12474 /* See if we already have the appropriately qualified variant of
12475 this type. */
12476 qualified_type = get_qualified_type (type, cv_quals);
12477
12478 if (qualified_type == sizetype)
12479 {
12480 /* Try not to expose the internal sizetype type's name. */
12481 if (TYPE_NAME (qualified_type)
12482 && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
12483 {
12484 tree t = TREE_TYPE (TYPE_NAME (qualified_type));
12485
12486 gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
12487 && (TYPE_PRECISION (t)
12488 == TYPE_PRECISION (qualified_type))
12489 && (TYPE_UNSIGNED (t)
12490 == TYPE_UNSIGNED (qualified_type)));
12491 qualified_type = t;
12492 }
12493 else if (qualified_type == sizetype
12494 && TREE_CODE (sizetype) == TREE_CODE (size_type_node)
12495 && TYPE_PRECISION (sizetype) == TYPE_PRECISION (size_type_node)
12496 && TYPE_UNSIGNED (sizetype) == TYPE_UNSIGNED (size_type_node))
12497 qualified_type = size_type_node;
12498 }
12499
12500
12501 /* If we do, then we can just use its DIE, if it exists. */
12502 if (qualified_type)
12503 {
12504 mod_type_die = lookup_type_die (qualified_type);
12505
12506 /* DW_AT_endianity doesn't come from a qualifier on the type. */
12507 if (mod_type_die
12508 && (!need_endianity_attribute_p (reverse)
12509 || !is_base_type (type)
12510 || get_AT_unsigned (mod_type_die, DW_AT_endianity)))
12511 return mod_type_die;
12512 }
12513
12514 name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
12515
12516 /* Handle C typedef types. */
12517 if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
12518 && !DECL_ARTIFICIAL (name))
12519 {
12520 tree dtype = TREE_TYPE (name);
12521
12522 if (qualified_type == dtype)
12523 {
12524 tree origin = decl_ultimate_origin (name);
12525
12526 /* Typedef variants that have an abstract origin don't get their own
12527 type DIE (see gen_typedef_die), so fall back on the ultimate
12528 abstract origin instead. */
12529 if (origin != NULL)
12530 return modified_type_die (TREE_TYPE (origin), cv_quals, reverse,
12531 context_die);
12532
12533 /* For a named type, use the typedef. */
12534 gen_type_die (qualified_type, context_die);
12535 return lookup_type_die (qualified_type);
12536 }
12537 else
12538 {
12539 int dquals = TYPE_QUALS_NO_ADDR_SPACE (dtype);
12540 dquals &= cv_qual_mask;
12541 if ((dquals & ~cv_quals) != TYPE_UNQUALIFIED
12542 || (cv_quals == dquals && DECL_ORIGINAL_TYPE (name) != type))
12543 /* cv-unqualified version of named type. Just use
12544 the unnamed type to which it refers. */
12545 return modified_type_die (DECL_ORIGINAL_TYPE (name), cv_quals,
12546 reverse, context_die);
12547 /* Else cv-qualified version of named type; fall through. */
12548 }
12549 }
12550
12551 mod_scope = scope_die_for (type, context_die);
12552
12553 if (cv_quals)
12554 {
12555 int sub_quals = 0, first_quals = 0;
12556 unsigned i;
12557 dw_die_ref first = NULL, last = NULL;
12558
12559 /* Determine a lesser qualified type that most closely matches
12560 this one. Then generate DW_TAG_* entries for the remaining
12561 qualifiers. */
12562 sub_quals = get_nearest_type_subqualifiers (type, cv_quals,
12563 cv_qual_mask);
12564 if (sub_quals && use_debug_types)
12565 {
12566 bool needed = false;
12567 /* If emitting type units, make sure the order of qualifiers
12568 is canonical. Thus, start from unqualified type if
12569 an earlier qualifier is missing in sub_quals, but some later
12570 one is present there. */
12571 for (i = 0; i < dwarf_qual_info_size; i++)
12572 if (dwarf_qual_info[i].q & cv_quals & ~sub_quals)
12573 needed = true;
12574 else if (needed && (dwarf_qual_info[i].q & cv_quals))
12575 {
12576 sub_quals = 0;
12577 break;
12578 }
12579 }
12580 mod_type_die = modified_type_die (type, sub_quals, reverse, context_die);
12581 if (mod_scope && mod_type_die && mod_type_die->die_parent == mod_scope)
12582 {
12583 /* As not all intermediate qualified DIEs have corresponding
12584 tree types, ensure that qualified DIEs in the same scope
12585 as their DW_AT_type are emitted after their DW_AT_type,
12586 only with other qualified DIEs for the same type possibly
12587 in between them. Determine the range of such qualified
12588 DIEs now (first being the base type, last being corresponding
12589 last qualified DIE for it). */
12590 unsigned int count = 0;
12591 first = qualified_die_p (mod_type_die, &first_quals,
12592 dwarf_qual_info_size);
12593 if (first == NULL)
12594 first = mod_type_die;
12595 gcc_assert ((first_quals & ~sub_quals) == 0);
12596 for (count = 0, last = first;
12597 count < (1U << dwarf_qual_info_size);
12598 count++, last = last->die_sib)
12599 {
12600 int quals = 0;
12601 if (last == mod_scope->die_child)
12602 break;
12603 if (qualified_die_p (last->die_sib, &quals, dwarf_qual_info_size)
12604 != first)
12605 break;
12606 }
12607 }
12608
12609 for (i = 0; i < dwarf_qual_info_size; i++)
12610 if (dwarf_qual_info[i].q & cv_quals & ~sub_quals)
12611 {
12612 dw_die_ref d;
12613 if (first && first != last)
12614 {
12615 for (d = first->die_sib; ; d = d->die_sib)
12616 {
12617 int quals = 0;
12618 qualified_die_p (d, &quals, dwarf_qual_info_size);
12619 if (quals == (first_quals | dwarf_qual_info[i].q))
12620 break;
12621 if (d == last)
12622 {
12623 d = NULL;
12624 break;
12625 }
12626 }
12627 if (d)
12628 {
12629 mod_type_die = d;
12630 continue;
12631 }
12632 }
12633 if (first)
12634 {
12635 d = ggc_cleared_alloc<die_node> ();
12636 d->die_tag = dwarf_qual_info[i].t;
12637 add_child_die_after (mod_scope, d, last);
12638 last = d;
12639 }
12640 else
12641 d = new_die (dwarf_qual_info[i].t, mod_scope, type);
12642 if (mod_type_die)
12643 add_AT_die_ref (d, DW_AT_type, mod_type_die);
12644 mod_type_die = d;
12645 first_quals |= dwarf_qual_info[i].q;
12646 }
12647 }
12648 else if (code == POINTER_TYPE || code == REFERENCE_TYPE)
12649 {
12650 dwarf_tag tag = DW_TAG_pointer_type;
12651 if (code == REFERENCE_TYPE)
12652 {
12653 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
12654 tag = DW_TAG_rvalue_reference_type;
12655 else
12656 tag = DW_TAG_reference_type;
12657 }
12658 mod_type_die = new_die (tag, mod_scope, type);
12659
12660 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
12661 simple_type_size_in_bits (type) / BITS_PER_UNIT);
12662 add_alignment_attribute (mod_type_die, type);
12663 item_type = TREE_TYPE (type);
12664
12665 addr_space_t as = TYPE_ADDR_SPACE (item_type);
12666 if (!ADDR_SPACE_GENERIC_P (as))
12667 {
12668 int action = targetm.addr_space.debug (as);
12669 if (action >= 0)
12670 {
12671 /* Positive values indicate an address_class. */
12672 add_AT_unsigned (mod_type_die, DW_AT_address_class, action);
12673 }
12674 else
12675 {
12676 /* Negative values indicate an (inverted) segment base reg. */
12677 dw_loc_descr_ref d
12678 = one_reg_loc_descriptor (~action, VAR_INIT_STATUS_INITIALIZED);
12679 add_AT_loc (mod_type_die, DW_AT_segment, d);
12680 }
12681 }
12682 }
12683 else if (code == INTEGER_TYPE
12684 && TREE_TYPE (type) != NULL_TREE
12685 && subrange_type_for_debug_p (type, &low, &high))
12686 {
12687 tree bias = NULL_TREE;
12688 if (lang_hooks.types.get_type_bias)
12689 bias = lang_hooks.types.get_type_bias (type);
12690 mod_type_die = subrange_type_die (type, low, high, bias, context_die);
12691 item_type = TREE_TYPE (type);
12692 }
12693 else if (is_base_type (type))
12694 mod_type_die = base_type_die (type, reverse);
12695 else
12696 {
12697 gen_type_die (type, context_die);
12698
12699 /* We have to get the type_main_variant here (and pass that to the
12700 `lookup_type_die' routine) because the ..._TYPE node we have
12701 might simply be a *copy* of some original type node (where the
12702 copy was created to help us keep track of typedef names) and
12703 that copy might have a different TYPE_UID from the original
12704 ..._TYPE node. */
12705 if (TREE_CODE (type) == FUNCTION_TYPE
12706 || TREE_CODE (type) == METHOD_TYPE)
12707 {
12708 /* For function/method types, can't just use type_main_variant here,
12709 because that can have different ref-qualifiers for C++,
12710 but try to canonicalize. */
12711 tree main = TYPE_MAIN_VARIANT (type);
12712 for (tree t = main; t; t = TYPE_NEXT_VARIANT (t))
12713 if (TYPE_QUALS_NO_ADDR_SPACE (t) == 0
12714 && check_base_type (t, main)
12715 && check_lang_type (t, type))
12716 return lookup_type_die (t);
12717 return lookup_type_die (type);
12718 }
12719 else if (TREE_CODE (type) != VECTOR_TYPE
12720 && TREE_CODE (type) != ARRAY_TYPE)
12721 return lookup_type_die (type_main_variant (type));
12722 else
12723 /* Vectors have the debugging information in the type,
12724 not the main variant. */
12725 return lookup_type_die (type);
12726 }
12727
12728 /* Builtin types don't have a DECL_ORIGINAL_TYPE. For those,
12729 don't output a DW_TAG_typedef, since there isn't one in the
12730 user's program; just attach a DW_AT_name to the type.
12731 Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
12732 if the base type already has the same name. */
12733 if (name
12734 && ((TREE_CODE (name) != TYPE_DECL
12735 && (qualified_type == TYPE_MAIN_VARIANT (type)
12736 || (cv_quals == TYPE_UNQUALIFIED)))
12737 || (TREE_CODE (name) == TYPE_DECL
12738 && TREE_TYPE (name) == qualified_type
12739 && DECL_NAME (name))))
12740 {
12741 if (TREE_CODE (name) == TYPE_DECL)
12742 /* Could just call add_name_and_src_coords_attributes here,
12743 but since this is a builtin type it doesn't have any
12744 useful source coordinates anyway. */
12745 name = DECL_NAME (name);
12746 add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
12747 }
12748 /* This probably indicates a bug. */
12749 else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
12750 {
12751 name = TYPE_IDENTIFIER (type);
12752 add_name_attribute (mod_type_die,
12753 name ? IDENTIFIER_POINTER (name) : "__unknown__");
12754 }
12755
12756 if (qualified_type)
12757 equate_type_number_to_die (qualified_type, mod_type_die);
12758
12759 if (item_type)
12760 /* We must do this after the equate_type_number_to_die call, in case
12761 this is a recursive type. This ensures that the modified_type_die
12762 recursion will terminate even if the type is recursive. Recursive
12763 types are possible in Ada. */
12764 sub_die = modified_type_die (item_type,
12765 TYPE_QUALS_NO_ADDR_SPACE (item_type),
12766 reverse,
12767 context_die);
12768
12769 if (sub_die != NULL)
12770 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
12771
12772 add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
12773 if (TYPE_ARTIFICIAL (type))
12774 add_AT_flag (mod_type_die, DW_AT_artificial, 1);
12775
12776 return mod_type_die;
12777}
12778
12779/* Generate DIEs for the generic parameters of T.
12780 T must be either a generic type or a generic function.
12781 See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more. */
12782
12783static void
12784gen_generic_params_dies (tree t)
12785{
12786 tree parms, args;
12787 int parms_num, i;
12788 dw_die_ref die = NULL;
12789 int non_default;
12790
12791 if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
12792 return;
12793
12794 if (TYPE_P (t))
12795 die = lookup_type_die (t);
12796 else if (DECL_P (t))
12797 die = lookup_decl_die (t);
12798
12799 gcc_assert (die);
12800
12801 parms = lang_hooks.get_innermost_generic_parms (t);
12802 if (!parms)
12803 /* T has no generic parameter. It means T is neither a generic type
12804 or function. End of story. */
12805 return;
12806
12807 parms_num = TREE_VEC_LENGTH (parms);
12808 args = lang_hooks.get_innermost_generic_args (t);
12809 if (TREE_CHAIN (args) && TREE_CODE (TREE_CHAIN (args)) == INTEGER_CST)
12810 non_default = int_cst_value (TREE_CHAIN (args));
12811 else
12812 non_default = TREE_VEC_LENGTH (args);
12813 for (i = 0; i < parms_num; i++)
12814 {
12815 tree parm, arg, arg_pack_elems;
12816 dw_die_ref parm_die;
12817
12818 parm = TREE_VEC_ELT (parms, i);
12819 arg = TREE_VEC_ELT (args, i);
12820 arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
12821 gcc_assert (parm && TREE_VALUE (parm) && arg);
12822
12823 if (parm && TREE_VALUE (parm) && arg)
12824 {
12825 /* If PARM represents a template parameter pack,
12826 emit a DW_TAG_GNU_template_parameter_pack DIE, followed
12827 by DW_TAG_template_*_parameter DIEs for the argument
12828 pack elements of ARG. Note that ARG would then be
12829 an argument pack. */
12830 if (arg_pack_elems)
12831 parm_die = template_parameter_pack_die (TREE_VALUE (parm),
12832 arg_pack_elems,
12833 die);
12834 else
12835 parm_die = generic_parameter_die (TREE_VALUE (parm), arg,
12836 true /* emit name */, die);
12837 if (i >= non_default)
12838 add_AT_flag (parm_die, DW_AT_default_value, 1);
12839 }
12840 }
12841}
12842
12843/* Create and return a DIE for PARM which should be
12844 the representation of a generic type parameter.
12845 For instance, in the C++ front end, PARM would be a template parameter.
12846 ARG is the argument to PARM.
12847 EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
12848 name of the PARM.
12849 PARENT_DIE is the parent DIE which the new created DIE should be added to,
12850 as a child node. */
12851
12852static dw_die_ref
12853generic_parameter_die (tree parm, tree arg,
12854 bool emit_name_p,
12855 dw_die_ref parent_die)
12856{
12857 dw_die_ref tmpl_die = NULL;
12858 const char *name = NULL;
12859
12860 if (!parm || !DECL_NAME (parm) || !arg)
12861 return NULL;
12862
12863 /* We support non-type generic parameters and arguments,
12864 type generic parameters and arguments, as well as
12865 generic generic parameters (a.k.a. template template parameters in C++)
12866 and arguments. */
12867 if (TREE_CODE (parm) == PARM_DECL)
12868 /* PARM is a nontype generic parameter */
12869 tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
12870 else if (TREE_CODE (parm) == TYPE_DECL)
12871 /* PARM is a type generic parameter. */
12872 tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
12873 else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
12874 /* PARM is a generic generic parameter.
12875 Its DIE is a GNU extension. It shall have a
12876 DW_AT_name attribute to represent the name of the template template
12877 parameter, and a DW_AT_GNU_template_name attribute to represent the
12878 name of the template template argument. */
12879 tmpl_die = new_die (DW_TAG_GNU_template_template_param,
12880 parent_die, parm);
12881 else
12882 gcc_unreachable ();
12883
12884 if (tmpl_die)
12885 {
12886 tree tmpl_type;
12887
12888 /* If PARM is a generic parameter pack, it means we are
12889 emitting debug info for a template argument pack element.
12890 In other terms, ARG is a template argument pack element.
12891 In that case, we don't emit any DW_AT_name attribute for
12892 the die. */
12893 if (emit_name_p)
12894 {
12895 name = IDENTIFIER_POINTER (DECL_NAME (parm));
12896 gcc_assert (name);
12897 add_AT_string (tmpl_die, DW_AT_name, name);
12898 }
12899
12900 if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
12901 {
12902 /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
12903 TMPL_DIE should have a child DW_AT_type attribute that is set
12904 to the type of the argument to PARM, which is ARG.
12905 If PARM is a type generic parameter, TMPL_DIE should have a
12906 child DW_AT_type that is set to ARG. */
12907 tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
12908 add_type_attribute (tmpl_die, tmpl_type,
12909 (TREE_THIS_VOLATILE (tmpl_type)
12910 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED),
12911 false, parent_die);
12912 }
12913 else
12914 {
12915 /* So TMPL_DIE is a DIE representing a
12916 a generic generic template parameter, a.k.a template template
12917 parameter in C++ and arg is a template. */
12918
12919 /* The DW_AT_GNU_template_name attribute of the DIE must be set
12920 to the name of the argument. */
12921 name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
12922 if (name)
12923 add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
12924 }
12925
12926 if (TREE_CODE (parm) == PARM_DECL)
12927 /* So PARM is a non-type generic parameter.
12928 DWARF3 5.6.8 says we must set a DW_AT_const_value child
12929 attribute of TMPL_DIE which value represents the value
12930 of ARG.
12931 We must be careful here:
12932 The value of ARG might reference some function decls.
12933 We might currently be emitting debug info for a generic
12934 type and types are emitted before function decls, we don't
12935 know if the function decls referenced by ARG will actually be
12936 emitted after cgraph computations.
12937 So must defer the generation of the DW_AT_const_value to
12938 after cgraph is ready. */
12939 append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
12940 }
12941
12942 return tmpl_die;
12943}
12944
12945/* Generate and return a DW_TAG_GNU_template_parameter_pack DIE representing.
12946 PARM_PACK must be a template parameter pack. The returned DIE
12947 will be child DIE of PARENT_DIE. */
12948
12949static dw_die_ref
12950template_parameter_pack_die (tree parm_pack,
12951 tree parm_pack_args,
12952 dw_die_ref parent_die)
12953{
12954 dw_die_ref die;
12955 int j;
12956
12957 gcc_assert (parent_die && parm_pack);
12958
12959 die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
12960 add_name_and_src_coords_attributes (die, parm_pack);
12961 for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
12962 generic_parameter_die (parm_pack,
12963 TREE_VEC_ELT (parm_pack_args, j),
12964 false /* Don't emit DW_AT_name */,
12965 die);
12966 return die;
12967}
12968
12969/* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
12970 an enumerated type. */
12971
12972static inline int
12973type_is_enum (const_tree type)
12974{
12975 return TREE_CODE (type) == ENUMERAL_TYPE;
12976}
12977
12978/* Return the DBX register number described by a given RTL node. */
12979
12980static unsigned int
12981dbx_reg_number (const_rtx rtl)
12982{
12983 unsigned regno = REGNO (rtl);
12984
12985 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
12986
12987#ifdef LEAF_REG_REMAP
12988 if (crtl->uses_only_leaf_regs)
12989 {
12990 int leaf_reg = LEAF_REG_REMAP (regno);
12991 if (leaf_reg != -1)
12992 regno = (unsigned) leaf_reg;
12993 }
12994#endif
12995
12996 regno = DBX_REGISTER_NUMBER (regno);
12997 gcc_assert (regno != INVALID_REGNUM);
12998 return regno;
12999}
13000
13001/* Optionally add a DW_OP_piece term to a location description expression.
13002 DW_OP_piece is only added if the location description expression already
13003 doesn't end with DW_OP_piece. */
13004
13005static void
13006add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
13007{
13008 dw_loc_descr_ref loc;
13009
13010 if (*list_head != NULL)
13011 {
13012 /* Find the end of the chain. */
13013 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
13014 ;
13015
13016 if (loc->dw_loc_opc != DW_OP_piece)
13017 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
13018 }
13019}
13020
13021/* Return a location descriptor that designates a machine register or
13022 zero if there is none. */
13023
13024static dw_loc_descr_ref
13025reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
13026{
13027 rtx regs;
13028
13029 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
13030 return 0;
13031
13032 /* We only use "frame base" when we're sure we're talking about the
13033 post-prologue local stack frame. We do this by *not* running
13034 register elimination until this point, and recognizing the special
13035 argument pointer and soft frame pointer rtx's.
13036 Use DW_OP_fbreg offset DW_OP_stack_value in this case. */
13037 if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
13038 && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
13039 {
13040 dw_loc_descr_ref result = NULL;
13041
13042 if (dwarf_version >= 4 || !dwarf_strict)
13043 {
13044 result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
13045 initialized);
13046 if (result)
13047 add_loc_descr (&result,
13048 new_loc_descr (DW_OP_stack_value, 0, 0));
13049 }
13050 return result;
13051 }
13052
13053 regs = targetm.dwarf_register_span (rtl);
13054
13055 if (REG_NREGS (rtl) > 1 || regs)
13056 return multiple_reg_loc_descriptor (rtl, regs, initialized);
13057 else
13058 {
13059 unsigned int dbx_regnum = dbx_reg_number (rtl);
13060 if (dbx_regnum == IGNORED_DWARF_REGNUM)
13061 return 0;
13062 return one_reg_loc_descriptor (dbx_regnum, initialized);
13063 }
13064}
13065
13066/* Return a location descriptor that designates a machine register for
13067 a given hard register number. */
13068
13069static dw_loc_descr_ref
13070one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
13071{
13072 dw_loc_descr_ref reg_loc_descr;
13073
13074 if (regno <= 31)
13075 reg_loc_descr
13076 = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
13077 else
13078 reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
13079
13080 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13081 add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13082
13083 return reg_loc_descr;
13084}
13085
13086/* Given an RTL of a register, return a location descriptor that
13087 designates a value that spans more than one register. */
13088
13089static dw_loc_descr_ref
13090multiple_reg_loc_descriptor (rtx rtl, rtx regs,
13091 enum var_init_status initialized)
13092{
13093 int size, i;
13094 dw_loc_descr_ref loc_result = NULL;
13095
13096 /* Simple, contiguous registers. */
13097 if (regs == NULL_RTX)
13098 {
13099 unsigned reg = REGNO (rtl);
13100 int nregs;
13101
13102#ifdef LEAF_REG_REMAP
13103 if (crtl->uses_only_leaf_regs)
13104 {
13105 int leaf_reg = LEAF_REG_REMAP (reg);
13106 if (leaf_reg != -1)
13107 reg = (unsigned) leaf_reg;
13108 }
13109#endif
13110
13111 gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
13112 nregs = REG_NREGS (rtl);
13113
13114 size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
13115
13116 loc_result = NULL;
13117 while (nregs--)
13118 {
13119 dw_loc_descr_ref t;
13120
13121 t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
13122 VAR_INIT_STATUS_INITIALIZED);
13123 add_loc_descr (&loc_result, t);
13124 add_loc_descr_op_piece (&loc_result, size);
13125 ++reg;
13126 }
13127 return loc_result;
13128 }
13129
13130 /* Now onto stupid register sets in non contiguous locations. */
13131
13132 gcc_assert (GET_CODE (regs) == PARALLEL);
13133
13134 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
13135 loc_result = NULL;
13136
13137 for (i = 0; i < XVECLEN (regs, 0); ++i)
13138 {
13139 dw_loc_descr_ref t;
13140
13141 t = one_reg_loc_descriptor (dbx_reg_number (XVECEXP (regs, 0, i)),
13142 VAR_INIT_STATUS_INITIALIZED);
13143 add_loc_descr (&loc_result, t);
13144 add_loc_descr_op_piece (&loc_result, size);
13145 }
13146
13147 if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13148 add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13149 return loc_result;
13150}
13151
13152static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
13153
13154/* Return a location descriptor that designates a constant i,
13155 as a compound operation from constant (i >> shift), constant shift
13156 and DW_OP_shl. */
13157
13158static dw_loc_descr_ref
13159int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
13160{
13161 dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
13162 add_loc_descr (&ret, int_loc_descriptor (shift));
13163 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
13164 return ret;
13165}
13166
13167/* Return a location descriptor that designates a constant. */
13168
13169static dw_loc_descr_ref
13170int_loc_descriptor (HOST_WIDE_INT i)
13171{
13172 enum dwarf_location_atom op;
13173
13174 /* Pick the smallest representation of a constant, rather than just
13175 defaulting to the LEB encoding. */
13176 if (i >= 0)
13177 {
13178 int clz = clz_hwi (i);
13179 int ctz = ctz_hwi (i);
13180 if (i <= 31)
13181 op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
13182 else if (i <= 0xff)
13183 op = DW_OP_const1u;
13184 else if (i <= 0xffff)
13185 op = DW_OP_const2u;
13186 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
13187 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
13188 /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
13189 DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
13190 while DW_OP_const4u is 5 bytes. */
13191 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
13192 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
13193 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
13194 /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
13195 while DW_OP_const4u is 5 bytes. */
13196 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
13197
13198 else if (DWARF2_ADDR_SIZE == 4 && i > 0x7fffffff
13199 && size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i)
13200 <= 4)
13201 {
13202 /* As i >= 2**31, the double cast above will yield a negative number.
13203 Since wrapping is defined in DWARF expressions we can output big
13204 positive integers as small negative ones, regardless of the size
13205 of host wide ints.
13206
13207 Here, since the evaluator will handle 32-bit values and since i >=
13208 2**31, we know it's going to be interpreted as a negative literal:
13209 store it this way if we can do better than 5 bytes this way. */
13210 return int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i);
13211 }
13212 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
13213 op = DW_OP_const4u;
13214
13215 /* Past this point, i >= 0x100000000 and thus DW_OP_constu will take at
13216 least 6 bytes: see if we can do better before falling back to it. */
13217 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
13218 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
13219 /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes. */
13220 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
13221 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
13222 && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
13223 >= HOST_BITS_PER_WIDE_INT)
13224 /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
13225 DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes. */
13226 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
13227 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
13228 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
13229 && size_of_uleb128 (i) > 6)
13230 /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes. */
13231 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
13232 else
13233 op = DW_OP_constu;
13234 }
13235 else
13236 {
13237 if (i >= -0x80)
13238 op = DW_OP_const1s;
13239 else if (i >= -0x8000)
13240 op = DW_OP_const2s;
13241 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
13242 {
13243 if (size_of_int_loc_descriptor (i) < 5)
13244 {
13245 dw_loc_descr_ref ret = int_loc_descriptor (-i);
13246 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
13247 return ret;
13248 }
13249 op = DW_OP_const4s;
13250 }
13251 else
13252 {
13253 if (size_of_int_loc_descriptor (i)
13254 < (unsigned long) 1 + size_of_sleb128 (i))
13255 {
13256 dw_loc_descr_ref ret = int_loc_descriptor (-i);
13257 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
13258 return ret;
13259 }
13260 op = DW_OP_consts;
13261 }
13262 }
13263
13264 return new_loc_descr (op, i, 0);
13265}
13266
13267/* Likewise, for unsigned constants. */
13268
13269static dw_loc_descr_ref
13270uint_loc_descriptor (unsigned HOST_WIDE_INT i)
13271{
13272 const unsigned HOST_WIDE_INT max_int = INTTYPE_MAXIMUM (HOST_WIDE_INT);
13273 const unsigned HOST_WIDE_INT max_uint
13274 = INTTYPE_MAXIMUM (unsigned HOST_WIDE_INT);
13275
13276 /* If possible, use the clever signed constants handling. */
13277 if (i <= max_int)
13278 return int_loc_descriptor ((HOST_WIDE_INT) i);
13279
13280 /* Here, we are left with positive numbers that cannot be represented as
13281 HOST_WIDE_INT, i.e.:
13282 max (HOST_WIDE_INT) < i <= max (unsigned HOST_WIDE_INT)
13283
13284 Using DW_OP_const4/8/./u operation to encode them consumes a lot of bytes
13285 whereas may be better to output a negative integer: thanks to integer
13286 wrapping, we know that:
13287 x = x - 2 ** DWARF2_ADDR_SIZE
13288 = x - 2 * (max (HOST_WIDE_INT) + 1)
13289 So numbers close to max (unsigned HOST_WIDE_INT) could be represented as
13290 small negative integers. Let's try that in cases it will clearly improve
13291 the encoding: there is no gain turning DW_OP_const4u into
13292 DW_OP_const4s. */
13293 if (DWARF2_ADDR_SIZE * 8 == HOST_BITS_PER_WIDE_INT
13294 && ((DWARF2_ADDR_SIZE == 4 && i > max_uint - 0x8000)
13295 || (DWARF2_ADDR_SIZE == 8 && i > max_uint - 0x80000000)))
13296 {
13297 const unsigned HOST_WIDE_INT first_shift = i - max_int - 1;
13298
13299 /* Now, -1 < first_shift <= max (HOST_WIDE_INT)
13300 i.e. 0 <= first_shift <= max (HOST_WIDE_INT). */
13301 const HOST_WIDE_INT second_shift
13302 = (HOST_WIDE_INT) first_shift - (HOST_WIDE_INT) max_int - 1;
13303
13304 /* So we finally have:
13305 -max (HOST_WIDE_INT) - 1 <= second_shift <= -1.
13306 i.e. min (HOST_WIDE_INT) <= second_shift < 0. */
13307 return int_loc_descriptor (second_shift);
13308 }
13309
13310 /* Last chance: fallback to a simple constant operation. */
13311 return new_loc_descr
13312 ((HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
13313 ? DW_OP_const4u
13314 : DW_OP_const8u,
13315 i, 0);
13316}
13317
13318/* Generate and return a location description that computes the unsigned
13319 comparison of the two stack top entries (a OP b where b is the top-most
13320 entry and a is the second one). The KIND of comparison can be LT_EXPR,
13321 LE_EXPR, GT_EXPR or GE_EXPR. */
13322
13323static dw_loc_descr_ref
13324uint_comparison_loc_list (enum tree_code kind)
13325{
13326 enum dwarf_location_atom op, flip_op;
13327 dw_loc_descr_ref ret, bra_node, jmp_node, tmp;
13328
13329 switch (kind)
13330 {
13331 case LT_EXPR:
13332 op = DW_OP_lt;
13333 break;
13334 case LE_EXPR:
13335 op = DW_OP_le;
13336 break;
13337 case GT_EXPR:
13338 op = DW_OP_gt;
13339 break;
13340 case GE_EXPR:
13341 op = DW_OP_ge;
13342 break;
13343 default:
13344 gcc_unreachable ();
13345 }
13346
13347 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
13348 jmp_node = new_loc_descr (DW_OP_skip, 0, 0);
13349
13350 /* Until DWARFv4, operations all work on signed integers. It is nevertheless
13351 possible to perform unsigned comparisons: we just have to distinguish
13352 three cases:
13353
13354 1. when a and b have the same sign (as signed integers); then we should
13355 return: a OP(signed) b;
13356
13357 2. when a is a negative signed integer while b is a positive one, then a
13358 is a greater unsigned integer than b; likewise when a and b's roles
13359 are flipped.
13360
13361 So first, compare the sign of the two operands. */
13362 ret = new_loc_descr (DW_OP_over, 0, 0);
13363 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
13364 add_loc_descr (&ret, new_loc_descr (DW_OP_xor, 0, 0));
13365 /* If they have different signs (i.e. they have different sign bits), then
13366 the stack top value has now the sign bit set and thus it's smaller than
13367 zero. */
13368 add_loc_descr (&ret, new_loc_descr (DW_OP_lit0, 0, 0));
13369 add_loc_descr (&ret, new_loc_descr (DW_OP_lt, 0, 0));
13370 add_loc_descr (&ret, bra_node);
13371
13372 /* We are in case 1. At this point, we know both operands have the same
13373 sign, to it's safe to use the built-in signed comparison. */
13374 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
13375 add_loc_descr (&ret, jmp_node);
13376
13377 /* We are in case 2. Here, we know both operands do not have the same sign,
13378 so we have to flip the signed comparison. */
13379 flip_op = (kind == LT_EXPR || kind == LE_EXPR) ? DW_OP_gt : DW_OP_lt;
13380 tmp = new_loc_descr (flip_op, 0, 0);
13381 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13382 bra_node->dw_loc_oprnd1.v.val_loc = tmp;
13383 add_loc_descr (&ret, tmp);
13384
13385 /* This dummy operation is necessary to make the two branches join. */
13386 tmp = new_loc_descr (DW_OP_nop, 0, 0);
13387 jmp_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13388 jmp_node->dw_loc_oprnd1.v.val_loc = tmp;
13389 add_loc_descr (&ret, tmp);
13390
13391 return ret;
13392}
13393
13394/* Likewise, but takes the location description lists (might be destructive on
13395 them). Return NULL if either is NULL or if concatenation fails. */
13396
13397static dw_loc_list_ref
13398loc_list_from_uint_comparison (dw_loc_list_ref left, dw_loc_list_ref right,
13399 enum tree_code kind)
13400{
13401 if (left == NULL || right == NULL)
13402 return NULL;
13403
13404 add_loc_list (&left, right);
13405 if (left == NULL)
13406 return NULL;
13407
13408 add_loc_descr_to_each (left, uint_comparison_loc_list (kind));
13409 return left;
13410}
13411
13412/* Return size_of_locs (int_shift_loc_descriptor (i, shift))
13413 without actually allocating it. */
13414
13415static unsigned long
13416size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
13417{
13418 return size_of_int_loc_descriptor (i >> shift)
13419 + size_of_int_loc_descriptor (shift)
13420 + 1;
13421}
13422
13423/* Return size_of_locs (int_loc_descriptor (i)) without
13424 actually allocating it. */
13425
13426static unsigned long
13427size_of_int_loc_descriptor (HOST_WIDE_INT i)
13428{
13429 unsigned long s;
13430
13431 if (i >= 0)
13432 {
13433 int clz, ctz;
13434 if (i <= 31)
13435 return 1;
13436 else if (i <= 0xff)
13437 return 2;
13438 else if (i <= 0xffff)
13439 return 3;
13440 clz = clz_hwi (i);
13441 ctz = ctz_hwi (i);
13442 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
13443 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
13444 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13445 - clz - 5);
13446 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
13447 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
13448 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13449 - clz - 8);
13450 else if (DWARF2_ADDR_SIZE == 4 && i > 0x7fffffff
13451 && size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i)
13452 <= 4)
13453 return size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i);
13454 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
13455 return 5;
13456 s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
13457 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
13458 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
13459 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13460 - clz - 8);
13461 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
13462 && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
13463 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13464 - clz - 16);
13465 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
13466 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
13467 && s > 6)
13468 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13469 - clz - 32);
13470 else
13471 return 1 + s;
13472 }
13473 else
13474 {
13475 if (i >= -0x80)
13476 return 2;
13477 else if (i >= -0x8000)
13478 return 3;
13479 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
13480 {
13481 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
13482 {
13483 s = size_of_int_loc_descriptor (-i) + 1;
13484 if (s < 5)
13485 return s;
13486 }
13487 return 5;
13488 }
13489 else
13490 {
13491 unsigned long r = 1 + size_of_sleb128 (i);
13492 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
13493 {
13494 s = size_of_int_loc_descriptor (-i) + 1;
13495 if (s < r)
13496 return s;
13497 }
13498 return r;
13499 }
13500 }
13501}
13502
13503/* Return loc description representing "address" of integer value.
13504 This can appear only as toplevel expression. */
13505
13506static dw_loc_descr_ref
13507address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
13508{
13509 int litsize;
13510 dw_loc_descr_ref loc_result = NULL;
13511
13512 if (!(dwarf_version >= 4 || !dwarf_strict))
13513 return NULL;
13514
13515 litsize = size_of_int_loc_descriptor (i);
13516 /* Determine if DW_OP_stack_value or DW_OP_implicit_value
13517 is more compact. For DW_OP_stack_value we need:
13518 litsize + 1 (DW_OP_stack_value)
13519 and for DW_OP_implicit_value:
13520 1 (DW_OP_implicit_value) + 1 (length) + size. */
13521 if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
13522 {
13523 loc_result = int_loc_descriptor (i);
13524 add_loc_descr (&loc_result,
13525 new_loc_descr (DW_OP_stack_value, 0, 0));
13526 return loc_result;
13527 }
13528
13529 loc_result = new_loc_descr (DW_OP_implicit_value,
13530 size, 0);
13531 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
13532 loc_result->dw_loc_oprnd2.v.val_int = i;
13533 return loc_result;
13534}
13535
13536/* Return a location descriptor that designates a base+offset location. */
13537
13538static dw_loc_descr_ref
13539based_loc_descr (rtx reg, HOST_WIDE_INT offset,
13540 enum var_init_status initialized)
13541{
13542 unsigned int regno;
13543 dw_loc_descr_ref result;
13544 dw_fde_ref fde = cfun->fde;
13545
13546 /* We only use "frame base" when we're sure we're talking about the
13547 post-prologue local stack frame. We do this by *not* running
13548 register elimination until this point, and recognizing the special
13549 argument pointer and soft frame pointer rtx's. */
13550 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
13551 {
13552 rtx elim = (ira_use_lra_p
13553 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
13554 : eliminate_regs (reg, VOIDmode, NULL_RTX));
13555
13556 if (elim != reg)
13557 {
13558 if (GET_CODE (elim) == PLUS)
13559 {
13560 offset += INTVAL (XEXP (elim, 1));
13561 elim = XEXP (elim, 0);
13562 }
13563 gcc_assert ((SUPPORTS_STACK_ALIGNMENT
13564 && (elim == hard_frame_pointer_rtx
13565 || elim == stack_pointer_rtx))
13566 || elim == (frame_pointer_needed
13567 ? hard_frame_pointer_rtx
13568 : stack_pointer_rtx));
13569
13570 /* If drap register is used to align stack, use frame
13571 pointer + offset to access stack variables. If stack
13572 is aligned without drap, use stack pointer + offset to
13573 access stack variables. */
13574 if (crtl->stack_realign_tried
13575 && reg == frame_pointer_rtx)
13576 {
13577 int base_reg
13578 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
13579 ? HARD_FRAME_POINTER_REGNUM
13580 : REGNO (elim));
13581 return new_reg_loc_descr (base_reg, offset);
13582 }
13583
13584 gcc_assert (frame_pointer_fb_offset_valid);
13585 offset += frame_pointer_fb_offset;
13586 return new_loc_descr (DW_OP_fbreg, offset, 0);
13587 }
13588 }
13589
13590 regno = REGNO (reg);
13591#ifdef LEAF_REG_REMAP
13592 if (crtl->uses_only_leaf_regs)
13593 {
13594 int leaf_reg = LEAF_REG_REMAP (regno);
13595 if (leaf_reg != -1)
13596 regno = (unsigned) leaf_reg;
13597 }
13598#endif
13599 regno = DWARF_FRAME_REGNUM (regno);
13600
13601 if (!optimize && fde
13602 && (fde->drap_reg == regno || fde->vdrap_reg == regno))
13603 {
13604 /* Use cfa+offset to represent the location of arguments passed
13605 on the stack when drap is used to align stack.
13606 Only do this when not optimizing, for optimized code var-tracking
13607 is supposed to track where the arguments live and the register
13608 used as vdrap or drap in some spot might be used for something
13609 else in other part of the routine. */
13610 return new_loc_descr (DW_OP_fbreg, offset, 0);
13611 }
13612
13613 if (regno <= 31)
13614 result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
13615 offset, 0);
13616 else
13617 result = new_loc_descr (DW_OP_bregx, regno, offset);
13618
13619 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13620 add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13621
13622 return result;
13623}
13624
13625/* Return true if this RTL expression describes a base+offset calculation. */
13626
13627static inline int
13628is_based_loc (const_rtx rtl)
13629{
13630 return (GET_CODE (rtl) == PLUS
13631 && ((REG_P (XEXP (rtl, 0))
13632 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
13633 && CONST_INT_P (XEXP (rtl, 1)))));
13634}
13635
13636/* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
13637 failed. */
13638
13639static dw_loc_descr_ref
13640tls_mem_loc_descriptor (rtx mem)
13641{
13642 tree base;
13643 dw_loc_descr_ref loc_result;
13644
13645 if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
13646 return NULL;
13647
13648 base = get_base_address (MEM_EXPR (mem));
13649 if (base == NULL
13650 || !VAR_P (base)
13651 || !DECL_THREAD_LOCAL_P (base))
13652 return NULL;
13653
13654 loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1, NULL);
13655 if (loc_result == NULL)
13656 return NULL;
13657
13658 if (MEM_OFFSET (mem))
13659 loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
13660
13661 return loc_result;
13662}
13663
13664/* Output debug info about reason why we failed to expand expression as dwarf
13665 expression. */
13666
13667static void
13668expansion_failed (tree expr, rtx rtl, char const *reason)
13669{
13670 if (dump_file && (dump_flags & TDF_DETAILS))
13671 {
13672 fprintf (dump_file, "Failed to expand as dwarf: ");
13673 if (expr)
13674 print_generic_expr (dump_file, expr, dump_flags);
13675 if (rtl)
13676 {
13677 fprintf (dump_file, "\n");
13678 print_rtl (dump_file, rtl);
13679 }
13680 fprintf (dump_file, "\nReason: %s\n", reason);
13681 }
13682}
13683
13684/* Helper function for const_ok_for_output. */
13685
13686static bool
13687const_ok_for_output_1 (rtx rtl)
13688{
13689 if (GET_CODE (rtl) == UNSPEC)
13690 {
13691 /* If delegitimize_address couldn't do anything with the UNSPEC, assume
13692 we can't express it in the debug info. */
13693 /* Don't complain about TLS UNSPECs, those are just too hard to
13694 delegitimize. Note this could be a non-decl SYMBOL_REF such as
13695 one in a constant pool entry, so testing SYMBOL_REF_TLS_MODEL
13696 rather than DECL_THREAD_LOCAL_P is not just an optimization. */
13697 if (flag_checking
13698 && (XVECLEN (rtl, 0) == 0
13699 || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
13700 || SYMBOL_REF_TLS_MODEL (XVECEXP (rtl, 0, 0)) == TLS_MODEL_NONE))
13701 inform (current_function_decl
13702 ? DECL_SOURCE_LOCATION (current_function_decl)
13703 : UNKNOWN_LOCATION,
13704#if NUM_UNSPEC_VALUES > 0
13705 "non-delegitimized UNSPEC %s (%d) found in variable location",
13706 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
13707 ? unspec_strings[XINT (rtl, 1)] : "unknown"),
13708 XINT (rtl, 1));
13709#else
13710 "non-delegitimized UNSPEC %d found in variable location",
13711 XINT (rtl, 1));
13712#endif
13713 expansion_failed (NULL_TREE, rtl,
13714 "UNSPEC hasn't been delegitimized.\n");
13715 return false;
13716 }
13717
13718 if (targetm.const_not_ok_for_debug_p (rtl))
13719 {
13720 expansion_failed (NULL_TREE, rtl,
13721 "Expression rejected for debug by the backend.\n");
13722 return false;
13723 }
13724
13725 /* FIXME: Refer to PR60655. It is possible for simplification
13726 of rtl expressions in var tracking to produce such expressions.
13727 We should really identify / validate expressions
13728 enclosed in CONST that can be handled by assemblers on various
13729 targets and only handle legitimate cases here. */
13730 if (GET_CODE (rtl) != SYMBOL_REF)
13731 {
13732 if (GET_CODE (rtl) == NOT)
13733 return false;
13734 return true;
13735 }
13736
13737 if (CONSTANT_POOL_ADDRESS_P (rtl))
13738 {
13739 bool marked;
13740 get_pool_constant_mark (rtl, &marked);
13741 /* If all references to this pool constant were optimized away,
13742 it was not output and thus we can't represent it. */
13743 if (!marked)
13744 {
13745 expansion_failed (NULL_TREE, rtl,
13746 "Constant was removed from constant pool.\n");
13747 return false;
13748 }
13749 }
13750
13751 if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
13752 return false;
13753
13754 /* Avoid references to external symbols in debug info, on several targets
13755 the linker might even refuse to link when linking a shared library,
13756 and in many other cases the relocations for .debug_info/.debug_loc are
13757 dropped, so the address becomes zero anyway. Hidden symbols, guaranteed
13758 to be defined within the same shared library or executable are fine. */
13759 if (SYMBOL_REF_EXTERNAL_P (rtl))
13760 {
13761 tree decl = SYMBOL_REF_DECL (rtl);
13762
13763 if (decl == NULL || !targetm.binds_local_p (decl))
13764 {
13765 expansion_failed (NULL_TREE, rtl,
13766 "Symbol not defined in current TU.\n");
13767 return false;
13768 }
13769 }
13770
13771 return true;
13772}
13773
13774/* Return true if constant RTL can be emitted in DW_OP_addr or
13775 DW_AT_const_value. TLS SYMBOL_REFs, external SYMBOL_REFs or
13776 non-marked constant pool SYMBOL_REFs can't be referenced in it. */
13777
13778static bool
13779const_ok_for_output (rtx rtl)
13780{
13781 if (GET_CODE (rtl) == SYMBOL_REF)
13782 return const_ok_for_output_1 (rtl);
13783
13784 if (GET_CODE (rtl) == CONST)
13785 {
13786 subrtx_var_iterator::array_type array;
13787 FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 0), ALL)
13788 if (!const_ok_for_output_1 (*iter))
13789 return false;
13790 return true;
13791 }
13792
13793 return true;
13794}
13795
13796/* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
13797 if possible, NULL otherwise. */
13798
13799static dw_die_ref
13800base_type_for_mode (machine_mode mode, bool unsignedp)
13801{
13802 dw_die_ref type_die;
13803 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
13804
13805 if (type == NULL)
13806 return NULL;
13807 switch (TREE_CODE (type))
13808 {
13809 case INTEGER_TYPE:
13810 case REAL_TYPE:
13811 break;
13812 default:
13813 return NULL;
13814 }
13815 type_die = lookup_type_die (type);
13816 if (!type_die)
13817 type_die = modified_type_die (type, TYPE_UNQUALIFIED, false,
13818 comp_unit_die ());
13819 if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
13820 return NULL;
13821 return type_die;
13822}
13823
13824/* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
13825 type matching MODE, or, if MODE is narrower than or as wide as
13826 DWARF2_ADDR_SIZE, untyped. Return NULL if the conversion is not
13827 possible. */
13828
13829static dw_loc_descr_ref
13830convert_descriptor_to_mode (machine_mode mode, dw_loc_descr_ref op)
13831{
13832 machine_mode outer_mode = mode;
13833 dw_die_ref type_die;
13834 dw_loc_descr_ref cvt;
13835
13836 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
13837 {
13838 add_loc_descr (&op, new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0));
13839 return op;
13840 }
13841 type_die = base_type_for_mode (outer_mode, 1);
13842 if (type_die == NULL)
13843 return NULL;
13844 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
13845 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13846 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13847 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13848 add_loc_descr (&op, cvt);
13849 return op;
13850}
13851
13852/* Return location descriptor for comparison OP with operands OP0 and OP1. */
13853
13854static dw_loc_descr_ref
13855compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
13856 dw_loc_descr_ref op1)
13857{
13858 dw_loc_descr_ref ret = op0;
13859 add_loc_descr (&ret, op1);
13860 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
13861 if (STORE_FLAG_VALUE != 1)
13862 {
13863 add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
13864 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
13865 }
13866 return ret;
13867}
13868
13869/* Return location descriptor for signed comparison OP RTL. */
13870
13871static dw_loc_descr_ref
13872scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
13873 machine_mode mem_mode)
13874{
13875 machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
13876 dw_loc_descr_ref op0, op1;
13877 int shift;
13878
13879 if (op_mode == VOIDmode)
13880 op_mode = GET_MODE (XEXP (rtl, 1));
13881 if (op_mode == VOIDmode)
13882 return NULL;
13883
13884 if (dwarf_strict
13885 && dwarf_version < 5
13886 && (!SCALAR_INT_MODE_P (op_mode)
13887 || GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE))
13888 return NULL;
13889
13890 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
13891 VAR_INIT_STATUS_INITIALIZED);
13892 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
13893 VAR_INIT_STATUS_INITIALIZED);
13894
13895 if (op0 == NULL || op1 == NULL)
13896 return NULL;
13897
13898 if (!SCALAR_INT_MODE_P (op_mode)
13899 || GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
13900 return compare_loc_descriptor (op, op0, op1);
13901
13902 if (GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
13903 {
13904 dw_die_ref type_die = base_type_for_mode (op_mode, 0);
13905 dw_loc_descr_ref cvt;
13906
13907 if (type_die == NULL)
13908 return NULL;
13909 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
13910 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13911 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13912 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13913 add_loc_descr (&op0, cvt);
13914 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
13915 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13916 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13917 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13918 add_loc_descr (&op1, cvt);
13919 return compare_loc_descriptor (op, op0, op1);
13920 }
13921
13922 shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
13923 /* For eq/ne, if the operands are known to be zero-extended,
13924 there is no need to do the fancy shifting up. */
13925 if (op == DW_OP_eq || op == DW_OP_ne)
13926 {
13927 dw_loc_descr_ref last0, last1;
13928 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
13929 ;
13930 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
13931 ;
13932 /* deref_size zero extends, and for constants we can check
13933 whether they are zero extended or not. */
13934 if (((last0->dw_loc_opc == DW_OP_deref_size
13935 && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
13936 || (CONST_INT_P (XEXP (rtl, 0))
13937 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
13938 == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
13939 && ((last1->dw_loc_opc == DW_OP_deref_size
13940 && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
13941 || (CONST_INT_P (XEXP (rtl, 1))
13942 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
13943 == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
13944 return compare_loc_descriptor (op, op0, op1);
13945
13946 /* EQ/NE comparison against constant in narrower type than
13947 DWARF2_ADDR_SIZE can be performed either as
13948 DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
13949 DW_OP_{eq,ne}
13950 or
13951 DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
13952 DW_OP_{eq,ne}. Pick whatever is shorter. */
13953 if (CONST_INT_P (XEXP (rtl, 1))
13954 && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
13955 && (size_of_int_loc_descriptor (shift) + 1
13956 + size_of_int_loc_descriptor (UINTVAL (XEXP (rtl, 1)) << shift)
13957 >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
13958 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
13959 & GET_MODE_MASK (op_mode))))
13960 {
13961 add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
13962 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
13963 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
13964 & GET_MODE_MASK (op_mode));
13965 return compare_loc_descriptor (op, op0, op1);
13966 }
13967 }
13968 add_loc_descr (&op0, int_loc_descriptor (shift));
13969 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
13970 if (CONST_INT_P (XEXP (rtl, 1)))
13971 op1 = int_loc_descriptor (UINTVAL (XEXP (rtl, 1)) << shift);
13972 else
13973 {
13974 add_loc_descr (&op1, int_loc_descriptor (shift));
13975 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
13976 }
13977 return compare_loc_descriptor (op, op0, op1);
13978}
13979
13980/* Return location descriptor for unsigned comparison OP RTL. */
13981
13982static dw_loc_descr_ref
13983ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
13984 machine_mode mem_mode)
13985{
13986 machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
13987 dw_loc_descr_ref op0, op1;
13988
13989 if (op_mode == VOIDmode)
13990 op_mode = GET_MODE (XEXP (rtl, 1));
13991 if (op_mode == VOIDmode)
13992 return NULL;
13993 if (!SCALAR_INT_MODE_P (op_mode))
13994 return NULL;
13995
13996 if (dwarf_strict
13997 && dwarf_version < 5
13998 && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
13999 return NULL;
14000
14001 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
14002 VAR_INIT_STATUS_INITIALIZED);
14003 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
14004 VAR_INIT_STATUS_INITIALIZED);
14005
14006 if (op0 == NULL || op1 == NULL)
14007 return NULL;
14008
14009 if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
14010 {
14011 HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
14012 dw_loc_descr_ref last0, last1;
14013 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
14014 ;
14015 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
14016 ;
14017 if (CONST_INT_P (XEXP (rtl, 0)))
14018 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
14019 /* deref_size zero extends, so no need to mask it again. */
14020 else if (last0->dw_loc_opc != DW_OP_deref_size
14021 || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
14022 {
14023 add_loc_descr (&op0, int_loc_descriptor (mask));
14024 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
14025 }
14026 if (CONST_INT_P (XEXP (rtl, 1)))
14027 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
14028 /* deref_size zero extends, so no need to mask it again. */
14029 else if (last1->dw_loc_opc != DW_OP_deref_size
14030 || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
14031 {
14032 add_loc_descr (&op1, int_loc_descriptor (mask));
14033 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
14034 }
14035 }
14036 else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
14037 {
14038 HOST_WIDE_INT bias = 1;
14039 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
14040 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14041 if (CONST_INT_P (XEXP (rtl, 1)))
14042 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
14043 + INTVAL (XEXP (rtl, 1)));
14044 else
14045 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
14046 bias, 0));
14047 }
14048 return compare_loc_descriptor (op, op0, op1);
14049}
14050
14051/* Return location descriptor for {U,S}{MIN,MAX}. */
14052
14053static dw_loc_descr_ref
14054minmax_loc_descriptor (rtx rtl, machine_mode mode,
14055 machine_mode mem_mode)
14056{
14057 enum dwarf_location_atom op;
14058 dw_loc_descr_ref op0, op1, ret;
14059 dw_loc_descr_ref bra_node, drop_node;
14060
14061 if (dwarf_strict
14062 && dwarf_version < 5
14063 && (!SCALAR_INT_MODE_P (mode)
14064 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE))
14065 return NULL;
14066
14067 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14068 VAR_INIT_STATUS_INITIALIZED);
14069 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
14070 VAR_INIT_STATUS_INITIALIZED);
14071
14072 if (op0 == NULL || op1 == NULL)
14073 return NULL;
14074
14075 add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
14076 add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
14077 add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
14078 if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
14079 {
14080 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
14081 {
14082 HOST_WIDE_INT mask = GET_MODE_MASK (mode);
14083 add_loc_descr (&op0, int_loc_descriptor (mask));
14084 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
14085 add_loc_descr (&op1, int_loc_descriptor (mask));
14086 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
14087 }
14088 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
14089 {
14090 HOST_WIDE_INT bias = 1;
14091 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
14092 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14093 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14094 }
14095 }
14096 else if (!SCALAR_INT_MODE_P (mode)
14097 && GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
14098 {
14099 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (mode)) * BITS_PER_UNIT;
14100 add_loc_descr (&op0, int_loc_descriptor (shift));
14101 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
14102 add_loc_descr (&op1, int_loc_descriptor (shift));
14103 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
14104 }
14105 else if (SCALAR_INT_MODE_P (mode)
14106 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
14107 {
14108 dw_die_ref type_die = base_type_for_mode (mode, 0);
14109 dw_loc_descr_ref cvt;
14110 if (type_die == NULL)
14111 return NULL;
14112 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14113 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14114 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14115 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14116 add_loc_descr (&op0, cvt);
14117 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14118 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14119 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14120 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14121 add_loc_descr (&op1, cvt);
14122 }
14123
14124 if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
14125 op = DW_OP_lt;
14126 else
14127 op = DW_OP_gt;
14128 ret = op0;
14129 add_loc_descr (&ret, op1);
14130 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
14131 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14132 add_loc_descr (&ret, bra_node);
14133 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14134 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
14135 add_loc_descr (&ret, drop_node);
14136 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14137 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
14138 if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
14139 && SCALAR_INT_MODE_P (mode)
14140 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
14141 ret = convert_descriptor_to_mode (mode, ret);
14142 return ret;
14143}
14144
14145/* Helper function for mem_loc_descriptor. Perform OP binary op,
14146 but after converting arguments to type_die, afterwards
14147 convert back to unsigned. */
14148
14149static dw_loc_descr_ref
14150typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
14151 machine_mode mode, machine_mode mem_mode)
14152{
14153 dw_loc_descr_ref cvt, op0, op1;
14154
14155 if (type_die == NULL)
14156 return NULL;
14157 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14158 VAR_INIT_STATUS_INITIALIZED);
14159 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
14160 VAR_INIT_STATUS_INITIALIZED);
14161 if (op0 == NULL || op1 == NULL)
14162 return NULL;
14163 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14164 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14165 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14166 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14167 add_loc_descr (&op0, cvt);
14168 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14169 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14170 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14171 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14172 add_loc_descr (&op1, cvt);
14173 add_loc_descr (&op0, op1);
14174 add_loc_descr (&op0, new_loc_descr (op, 0, 0));
14175 return convert_descriptor_to_mode (mode, op0);
14176}
14177
14178/* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
14179 const0 is DW_OP_lit0 or corresponding typed constant,
14180 const1 is DW_OP_lit1 or corresponding typed constant
14181 and constMSB is constant with just the MSB bit set
14182 for the mode):
14183 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
14184 L1: const0 DW_OP_swap
14185 L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
14186 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
14187 L3: DW_OP_drop
14188 L4: DW_OP_nop
14189
14190 CTZ is similar:
14191 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
14192 L1: const0 DW_OP_swap
14193 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
14194 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
14195 L3: DW_OP_drop
14196 L4: DW_OP_nop
14197
14198 FFS is similar:
14199 DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
14200 L1: const1 DW_OP_swap
14201 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
14202 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
14203 L3: DW_OP_drop
14204 L4: DW_OP_nop */
14205
14206static dw_loc_descr_ref
14207clz_loc_descriptor (rtx rtl, machine_mode mode,
14208 machine_mode mem_mode)
14209{
14210 dw_loc_descr_ref op0, ret, tmp;
14211 HOST_WIDE_INT valv;
14212 dw_loc_descr_ref l1jump, l1label;
14213 dw_loc_descr_ref l2jump, l2label;
14214 dw_loc_descr_ref l3jump, l3label;
14215 dw_loc_descr_ref l4jump, l4label;
14216 rtx msb;
14217
14218 if (!SCALAR_INT_MODE_P (mode)
14219 || GET_MODE (XEXP (rtl, 0)) != mode)
14220 return NULL;
14221
14222 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14223 VAR_INIT_STATUS_INITIALIZED);
14224 if (op0 == NULL)
14225 return NULL;
14226 ret = op0;
14227 if (GET_CODE (rtl) == CLZ)
14228 {
14229 if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
14230 valv = GET_MODE_BITSIZE (mode);
14231 }
14232 else if (GET_CODE (rtl) == FFS)
14233 valv = 0;
14234 else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
14235 valv = GET_MODE_BITSIZE (mode);
14236 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
14237 l1jump = new_loc_descr (DW_OP_bra, 0, 0);
14238 add_loc_descr (&ret, l1jump);
14239 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
14240 tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
14241 VAR_INIT_STATUS_INITIALIZED);
14242 if (tmp == NULL)
14243 return NULL;
14244 add_loc_descr (&ret, tmp);
14245 l4jump = new_loc_descr (DW_OP_skip, 0, 0);
14246 add_loc_descr (&ret, l4jump);
14247 l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
14248 ? const1_rtx : const0_rtx,
14249 mode, mem_mode,
14250 VAR_INIT_STATUS_INITIALIZED);
14251 if (l1label == NULL)
14252 return NULL;
14253 add_loc_descr (&ret, l1label);
14254 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14255 l2label = new_loc_descr (DW_OP_dup, 0, 0);
14256 add_loc_descr (&ret, l2label);
14257 if (GET_CODE (rtl) != CLZ)
14258 msb = const1_rtx;
14259 else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
14260 msb = GEN_INT (HOST_WIDE_INT_1U
14261 << (GET_MODE_BITSIZE (mode) - 1));
14262 else
14263 msb = immed_wide_int_const
14264 (wi::set_bit_in_zero (GET_MODE_PRECISION (mode) - 1,
14265 GET_MODE_PRECISION (mode)), mode);
14266 if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
14267 tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
14268 ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
14269 ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
14270 else
14271 tmp = mem_loc_descriptor (msb, mode, mem_mode,
14272 VAR_INIT_STATUS_INITIALIZED);
14273 if (tmp == NULL)
14274 return NULL;
14275 add_loc_descr (&ret, tmp);
14276 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
14277 l3jump = new_loc_descr (DW_OP_bra, 0, 0);
14278 add_loc_descr (&ret, l3jump);
14279 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
14280 VAR_INIT_STATUS_INITIALIZED);
14281 if (tmp == NULL)
14282 return NULL;
14283 add_loc_descr (&ret, tmp);
14284 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
14285 ? DW_OP_shl : DW_OP_shr, 0, 0));
14286 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14287 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
14288 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14289 l2jump = new_loc_descr (DW_OP_skip, 0, 0);
14290 add_loc_descr (&ret, l2jump);
14291 l3label = new_loc_descr (DW_OP_drop, 0, 0);
14292 add_loc_descr (&ret, l3label);
14293 l4label = new_loc_descr (DW_OP_nop, 0, 0);
14294 add_loc_descr (&ret, l4label);
14295 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14296 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
14297 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14298 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
14299 l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14300 l3jump->dw_loc_oprnd1.v.val_loc = l3label;
14301 l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14302 l4jump->dw_loc_oprnd1.v.val_loc = l4label;
14303 return ret;
14304}
14305
14306/* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
14307 const1 is DW_OP_lit1 or corresponding typed constant):
14308 const0 DW_OP_swap
14309 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
14310 DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
14311 L2: DW_OP_drop
14312
14313 PARITY is similar:
14314 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
14315 DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
14316 L2: DW_OP_drop */
14317
14318static dw_loc_descr_ref
14319popcount_loc_descriptor (rtx rtl, machine_mode mode,
14320 machine_mode mem_mode)
14321{
14322 dw_loc_descr_ref op0, ret, tmp;
14323 dw_loc_descr_ref l1jump, l1label;
14324 dw_loc_descr_ref l2jump, l2label;
14325
14326 if (!SCALAR_INT_MODE_P (mode)
14327 || GET_MODE (XEXP (rtl, 0)) != mode)
14328 return NULL;
14329
14330 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14331 VAR_INIT_STATUS_INITIALIZED);
14332 if (op0 == NULL)
14333 return NULL;
14334 ret = op0;
14335 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
14336 VAR_INIT_STATUS_INITIALIZED);
14337 if (tmp == NULL)
14338 return NULL;
14339 add_loc_descr (&ret, tmp);
14340 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14341 l1label = new_loc_descr (DW_OP_dup, 0, 0);
14342 add_loc_descr (&ret, l1label);
14343 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
14344 add_loc_descr (&ret, l2jump);
14345 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
14346 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
14347 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
14348 VAR_INIT_STATUS_INITIALIZED);
14349 if (tmp == NULL)
14350 return NULL;
14351 add_loc_descr (&ret, tmp);
14352 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
14353 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
14354 ? DW_OP_plus : DW_OP_xor, 0, 0));
14355 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14356 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
14357 VAR_INIT_STATUS_INITIALIZED);
14358 add_loc_descr (&ret, tmp);
14359 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
14360 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
14361 add_loc_descr (&ret, l1jump);
14362 l2label = new_loc_descr (DW_OP_drop, 0, 0);
14363 add_loc_descr (&ret, l2label);
14364 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14365 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
14366 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14367 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
14368 return ret;
14369}
14370
14371/* BSWAP (constS is initial shift count, either 56 or 24):
14372 constS const0
14373 L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
14374 const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
14375 DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
14376 DW_OP_minus DW_OP_swap DW_OP_skip <L1>
14377 L2: DW_OP_drop DW_OP_swap DW_OP_drop */
14378
14379static dw_loc_descr_ref
14380bswap_loc_descriptor (rtx rtl, machine_mode mode,
14381 machine_mode mem_mode)
14382{
14383 dw_loc_descr_ref op0, ret, tmp;
14384 dw_loc_descr_ref l1jump, l1label;
14385 dw_loc_descr_ref l2jump, l2label;
14386
14387 if (!SCALAR_INT_MODE_P (mode)
14388 || BITS_PER_UNIT != 8
14389 || (GET_MODE_BITSIZE (mode) != 32
14390 && GET_MODE_BITSIZE (mode) != 64))
14391 return NULL;
14392
14393 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14394 VAR_INIT_STATUS_INITIALIZED);
14395 if (op0 == NULL)
14396 return NULL;
14397
14398 ret = op0;
14399 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
14400 mode, mem_mode,
14401 VAR_INIT_STATUS_INITIALIZED);
14402 if (tmp == NULL)
14403 return NULL;
14404 add_loc_descr (&ret, tmp);
14405 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
14406 VAR_INIT_STATUS_INITIALIZED);
14407 if (tmp == NULL)
14408 return NULL;
14409 add_loc_descr (&ret, tmp);
14410 l1label = new_loc_descr (DW_OP_pick, 2, 0);
14411 add_loc_descr (&ret, l1label);
14412 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
14413 mode, mem_mode,
14414 VAR_INIT_STATUS_INITIALIZED);
14415 add_loc_descr (&ret, tmp);
14416 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
14417 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
14418 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
14419 tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
14420 VAR_INIT_STATUS_INITIALIZED);
14421 if (tmp == NULL)
14422 return NULL;
14423 add_loc_descr (&ret, tmp);
14424 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
14425 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
14426 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
14427 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
14428 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14429 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
14430 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
14431 VAR_INIT_STATUS_INITIALIZED);
14432 add_loc_descr (&ret, tmp);
14433 add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
14434 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
14435 add_loc_descr (&ret, l2jump);
14436 tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
14437 VAR_INIT_STATUS_INITIALIZED);
14438 add_loc_descr (&ret, tmp);
14439 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
14440 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14441 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
14442 add_loc_descr (&ret, l1jump);
14443 l2label = new_loc_descr (DW_OP_drop, 0, 0);
14444 add_loc_descr (&ret, l2label);
14445 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14446 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
14447 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14448 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
14449 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14450 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
14451 return ret;
14452}
14453
14454/* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
14455 DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
14456 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
14457 DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
14458
14459 ROTATERT is similar:
14460 DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
14461 DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
14462 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or */
14463
14464static dw_loc_descr_ref
14465rotate_loc_descriptor (rtx rtl, machine_mode mode,
14466 machine_mode mem_mode)
14467{
14468 rtx rtlop1 = XEXP (rtl, 1);
14469 dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
14470 int i;
14471
14472 if (!SCALAR_INT_MODE_P (mode))
14473 return NULL;
14474
14475 if (GET_MODE (rtlop1) != VOIDmode
14476 && GET_MODE_BITSIZE (GET_MODE (rtlop1)) < GET_MODE_BITSIZE (mode))
14477 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
14478 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14479 VAR_INIT_STATUS_INITIALIZED);
14480 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
14481 VAR_INIT_STATUS_INITIALIZED);
14482 if (op0 == NULL || op1 == NULL)
14483 return NULL;
14484 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
14485 for (i = 0; i < 2; i++)
14486 {
14487 if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
14488 mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
14489 mode, mem_mode,
14490 VAR_INIT_STATUS_INITIALIZED);
14491 else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
14492 mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
14493 ? DW_OP_const4u
14494 : HOST_BITS_PER_WIDE_INT == 64
14495 ? DW_OP_const8u : DW_OP_constu,
14496 GET_MODE_MASK (mode), 0);
14497 else
14498 mask[i] = NULL;
14499 if (mask[i] == NULL)
14500 return NULL;
14501 add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
14502 }
14503 ret = op0;
14504 add_loc_descr (&ret, op1);
14505 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
14506 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
14507 if (GET_CODE (rtl) == ROTATERT)
14508 {
14509 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
14510 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
14511 GET_MODE_BITSIZE (mode), 0));
14512 }
14513 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
14514 if (mask[0] != NULL)
14515 add_loc_descr (&ret, mask[0]);
14516 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
14517 if (mask[1] != NULL)
14518 {
14519 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14520 add_loc_descr (&ret, mask[1]);
14521 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14522 }
14523 if (GET_CODE (rtl) == ROTATE)
14524 {
14525 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
14526 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
14527 GET_MODE_BITSIZE (mode), 0));
14528 }
14529 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
14530 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
14531 return ret;
14532}
14533
14534/* Helper function for mem_loc_descriptor. Return DW_OP_GNU_parameter_ref
14535 for DEBUG_PARAMETER_REF RTL. */
14536
14537static dw_loc_descr_ref
14538parameter_ref_descriptor (rtx rtl)
14539{
14540 dw_loc_descr_ref ret;
14541 dw_die_ref ref;
14542
14543 if (dwarf_strict)
14544 return NULL;
14545 gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
14546 ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
14547 ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
14548 if (ref)
14549 {
14550 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14551 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
14552 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
14553 }
14554 else
14555 {
14556 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
14557 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
14558 }
14559 return ret;
14560}
14561
14562/* The following routine converts the RTL for a variable or parameter
14563 (resident in memory) into an equivalent Dwarf representation of a
14564 mechanism for getting the address of that same variable onto the top of a
14565 hypothetical "address evaluation" stack.
14566
14567 When creating memory location descriptors, we are effectively transforming
14568 the RTL for a memory-resident object into its Dwarf postfix expression
14569 equivalent. This routine recursively descends an RTL tree, turning
14570 it into Dwarf postfix code as it goes.
14571
14572 MODE is the mode that should be assumed for the rtl if it is VOIDmode.
14573
14574 MEM_MODE is the mode of the memory reference, needed to handle some
14575 autoincrement addressing modes.
14576
14577 Return 0 if we can't represent the location. */
14578
14579dw_loc_descr_ref
14580mem_loc_descriptor (rtx rtl, machine_mode mode,
14581 machine_mode mem_mode,
14582 enum var_init_status initialized)
14583{
14584 dw_loc_descr_ref mem_loc_result = NULL;
14585 enum dwarf_location_atom op;
14586 dw_loc_descr_ref op0, op1;
14587 rtx inner = NULL_RTX;
14588
14589 if (mode == VOIDmode)
14590 mode = GET_MODE (rtl);
14591
14592 /* Note that for a dynamically sized array, the location we will generate a
14593 description of here will be the lowest numbered location which is
14594 actually within the array. That's *not* necessarily the same as the
14595 zeroth element of the array. */
14596
14597 rtl = targetm.delegitimize_address (rtl);
14598
14599 if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
14600 return NULL;
14601
14602 switch (GET_CODE (rtl))
14603 {
14604 case POST_INC:
14605 case POST_DEC:
14606 case POST_MODIFY:
14607 return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
14608
14609 case SUBREG:
14610 /* The case of a subreg may arise when we have a local (register)
14611 variable or a formal (register) parameter which doesn't quite fill
14612 up an entire register. For now, just assume that it is
14613 legitimate to make the Dwarf info refer to the whole register which
14614 contains the given subreg. */
14615 if (!subreg_lowpart_p (rtl))
14616 break;
14617 inner = SUBREG_REG (rtl);
14618 /* FALLTHRU */
14619 case TRUNCATE:
14620 if (inner == NULL_RTX)
14621 inner = XEXP (rtl, 0);
14622 if (SCALAR_INT_MODE_P (mode)
14623 && SCALAR_INT_MODE_P (GET_MODE (inner))
14624 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
14625#ifdef POINTERS_EXTEND_UNSIGNED
14626 || (mode == Pmode && mem_mode != VOIDmode)
14627#endif
14628 )
14629 && GET_MODE_SIZE (GET_MODE (inner)) <= DWARF2_ADDR_SIZE)
14630 {
14631 mem_loc_result = mem_loc_descriptor (inner,
14632 GET_MODE (inner),
14633 mem_mode, initialized);
14634 break;
14635 }
14636 if (dwarf_strict && dwarf_version < 5)
14637 break;
14638 if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (inner)))
14639 break;
14640 if (GET_MODE_SIZE (mode) != GET_MODE_SIZE (GET_MODE (inner))
14641 && (!SCALAR_INT_MODE_P (mode)
14642 || !SCALAR_INT_MODE_P (GET_MODE (inner))))
14643 break;
14644 else
14645 {
14646 dw_die_ref type_die;
14647 dw_loc_descr_ref cvt;
14648
14649 mem_loc_result = mem_loc_descriptor (inner,
14650 GET_MODE (inner),
14651 mem_mode, initialized);
14652 if (mem_loc_result == NULL)
14653 break;
14654 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
14655 if (type_die == NULL)
14656 {
14657 mem_loc_result = NULL;
14658 break;
14659 }
14660 if (GET_MODE_SIZE (mode)
14661 != GET_MODE_SIZE (GET_MODE (inner)))
14662 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14663 else
14664 cvt = new_loc_descr (dwarf_OP (DW_OP_reinterpret), 0, 0);
14665 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14666 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14667 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14668 add_loc_descr (&mem_loc_result, cvt);
14669 if (SCALAR_INT_MODE_P (mode)
14670 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
14671 {
14672 /* Convert it to untyped afterwards. */
14673 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14674 add_loc_descr (&mem_loc_result, cvt);
14675 }
14676 }
14677 break;
14678
14679 case REG:
14680 if (! SCALAR_INT_MODE_P (mode)
14681 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
14682 && rtl != arg_pointer_rtx
14683 && rtl != frame_pointer_rtx
14684#ifdef POINTERS_EXTEND_UNSIGNED
14685 && (mode != Pmode || mem_mode == VOIDmode)
14686#endif
14687 ))
14688 {
14689 dw_die_ref type_die;
14690 unsigned int dbx_regnum;
14691
14692 if (dwarf_strict && dwarf_version < 5)
14693 break;
14694 if (REGNO (rtl) > FIRST_PSEUDO_REGISTER)
14695 break;
14696 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
14697 if (type_die == NULL)
14698 break;
14699
14700 dbx_regnum = dbx_reg_number (rtl);
14701 if (dbx_regnum == IGNORED_DWARF_REGNUM)
14702 break;
14703 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_regval_type),
14704 dbx_regnum, 0);
14705 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
14706 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
14707 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
14708 break;
14709 }
14710 /* Whenever a register number forms a part of the description of the
14711 method for calculating the (dynamic) address of a memory resident
14712 object, DWARF rules require the register number be referred to as
14713 a "base register". This distinction is not based in any way upon
14714 what category of register the hardware believes the given register
14715 belongs to. This is strictly DWARF terminology we're dealing with
14716 here. Note that in cases where the location of a memory-resident
14717 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
14718 OP_CONST (0)) the actual DWARF location descriptor that we generate
14719 may just be OP_BASEREG (basereg). This may look deceptively like
14720 the object in question was allocated to a register (rather than in
14721 memory) so DWARF consumers need to be aware of the subtle
14722 distinction between OP_REG and OP_BASEREG. */
14723 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
14724 mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
14725 else if (stack_realign_drap
14726 && crtl->drap_reg
14727 && crtl->args.internal_arg_pointer == rtl
14728 && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
14729 {
14730 /* If RTL is internal_arg_pointer, which has been optimized
14731 out, use DRAP instead. */
14732 mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
14733 VAR_INIT_STATUS_INITIALIZED);
14734 }
14735 break;
14736
14737 case SIGN_EXTEND:
14738 case ZERO_EXTEND:
14739 if (!SCALAR_INT_MODE_P (mode))
14740 break;
14741 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
14742 mem_mode, VAR_INIT_STATUS_INITIALIZED);
14743 if (op0 == 0)
14744 break;
14745 else if (GET_CODE (rtl) == ZERO_EXTEND
14746 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
14747 && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
14748 < HOST_BITS_PER_WIDE_INT
14749 /* If DW_OP_const{1,2,4}u won't be used, it is shorter
14750 to expand zero extend as two shifts instead of
14751 masking. */
14752 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= 4)
14753 {
14754 machine_mode imode = GET_MODE (XEXP (rtl, 0));
14755 mem_loc_result = op0;
14756 add_loc_descr (&mem_loc_result,
14757 int_loc_descriptor (GET_MODE_MASK (imode)));
14758 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
14759 }
14760 else if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
14761 {
14762 int shift = DWARF2_ADDR_SIZE
14763 - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
14764 shift *= BITS_PER_UNIT;
14765 if (GET_CODE (rtl) == SIGN_EXTEND)
14766 op = DW_OP_shra;
14767 else
14768 op = DW_OP_shr;
14769 mem_loc_result = op0;
14770 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
14771 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
14772 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
14773 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14774 }
14775 else if (!dwarf_strict || dwarf_version >= 5)
14776 {
14777 dw_die_ref type_die1, type_die2;
14778 dw_loc_descr_ref cvt;
14779
14780 type_die1 = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
14781 GET_CODE (rtl) == ZERO_EXTEND);
14782 if (type_die1 == NULL)
14783 break;
14784 type_die2 = base_type_for_mode (mode, 1);
14785 if (type_die2 == NULL)
14786 break;
14787 mem_loc_result = op0;
14788 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14789 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14790 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
14791 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14792 add_loc_descr (&mem_loc_result, cvt);
14793 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14794 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14795 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
14796 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14797 add_loc_descr (&mem_loc_result, cvt);
14798 }
14799 break;
14800
14801 case MEM:
14802 {
14803 rtx new_rtl = avoid_constant_pool_reference (rtl);
14804 if (new_rtl != rtl)
14805 {
14806 mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
14807 initialized);
14808 if (mem_loc_result != NULL)
14809 return mem_loc_result;
14810 }
14811 }
14812 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
14813 get_address_mode (rtl), mode,
14814 VAR_INIT_STATUS_INITIALIZED);
14815 if (mem_loc_result == NULL)
14816 mem_loc_result = tls_mem_loc_descriptor (rtl);
14817 if (mem_loc_result != NULL)
14818 {
14819 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
14820 || !SCALAR_INT_MODE_P(mode))
14821 {
14822 dw_die_ref type_die;
14823 dw_loc_descr_ref deref;
14824
14825 if (dwarf_strict && dwarf_version < 5)
14826 return NULL;
14827 type_die
14828 = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
14829 if (type_die == NULL)
14830 return NULL;
14831 deref = new_loc_descr (dwarf_OP (DW_OP_deref_type),
14832 GET_MODE_SIZE (mode), 0);
14833 deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
14834 deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
14835 deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
14836 add_loc_descr (&mem_loc_result, deref);
14837 }
14838 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
14839 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
14840 else
14841 add_loc_descr (&mem_loc_result,
14842 new_loc_descr (DW_OP_deref_size,
14843 GET_MODE_SIZE (mode), 0));
14844 }
14845 break;
14846
14847 case LO_SUM:
14848 return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
14849
14850 case LABEL_REF:
14851 /* Some ports can transform a symbol ref into a label ref, because
14852 the symbol ref is too far away and has to be dumped into a constant
14853 pool. */
14854 case CONST:
14855 case SYMBOL_REF:
14856 if (!SCALAR_INT_MODE_P (mode)
14857 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
14858#ifdef POINTERS_EXTEND_UNSIGNED
14859 && (mode != Pmode || mem_mode == VOIDmode)
14860#endif
14861 ))
14862 break;
14863 if (GET_CODE (rtl) == SYMBOL_REF
14864 && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
14865 {
14866 dw_loc_descr_ref temp;
14867
14868 /* If this is not defined, we have no way to emit the data. */
14869 if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
14870 break;
14871
14872 temp = new_addr_loc_descr (rtl, dtprel_true);
14873
14874 /* We check for DWARF 5 here because gdb did not implement
14875 DW_OP_form_tls_address until after 7.12. */
14876 mem_loc_result = new_loc_descr ((dwarf_version >= 5
14877 ? DW_OP_form_tls_address
14878 : DW_OP_GNU_push_tls_address),
14879 0, 0);
14880 add_loc_descr (&mem_loc_result, temp);
14881
14882 break;
14883 }
14884
14885 if (!const_ok_for_output (rtl))
14886 {
14887 if (GET_CODE (rtl) == CONST)
14888 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14889 initialized);
14890 break;
14891 }
14892
14893 symref:
14894 mem_loc_result = new_addr_loc_descr (rtl, dtprel_false);
14895 vec_safe_push (used_rtx_array, rtl);
14896 break;
14897
14898 case CONCAT:
14899 case CONCATN:
14900 case VAR_LOCATION:
14901 case DEBUG_IMPLICIT_PTR:
14902 expansion_failed (NULL_TREE, rtl,
14903 "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
14904 return 0;
14905
14906 case ENTRY_VALUE:
14907 if (dwarf_strict && dwarf_version < 5)
14908 return NULL;
14909 if (REG_P (ENTRY_VALUE_EXP (rtl)))
14910 {
14911 if (!SCALAR_INT_MODE_P (mode)
14912 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
14913 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
14914 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14915 else
14916 {
14917 unsigned int dbx_regnum = dbx_reg_number (ENTRY_VALUE_EXP (rtl));
14918 if (dbx_regnum == IGNORED_DWARF_REGNUM)
14919 return NULL;
14920 op0 = one_reg_loc_descriptor (dbx_regnum,
14921 VAR_INIT_STATUS_INITIALIZED);
14922 }
14923 }
14924 else if (MEM_P (ENTRY_VALUE_EXP (rtl))
14925 && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
14926 {
14927 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
14928 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14929 if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
14930 return NULL;
14931 }
14932 else
14933 gcc_unreachable ();
14934 if (op0 == NULL)
14935 return NULL;
14936 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_entry_value), 0, 0);
14937 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
14938 mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
14939 break;
14940
14941 case DEBUG_PARAMETER_REF:
14942 mem_loc_result = parameter_ref_descriptor (rtl);
14943 break;
14944
14945 case PRE_MODIFY:
14946 /* Extract the PLUS expression nested inside and fall into
14947 PLUS code below. */
14948 rtl = XEXP (rtl, 1);
14949 goto plus;
14950
14951 case PRE_INC:
14952 case PRE_DEC:
14953 /* Turn these into a PLUS expression and fall into the PLUS code
14954 below. */
14955 rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
14956 gen_int_mode (GET_CODE (rtl) == PRE_INC
14957 ? GET_MODE_UNIT_SIZE (mem_mode)
14958 : -GET_MODE_UNIT_SIZE (mem_mode),
14959 mode));
14960
14961 /* fall through */
14962
14963 case PLUS:
14964 plus:
14965 if (is_based_loc (rtl)
14966 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
14967 || XEXP (rtl, 0) == arg_pointer_rtx
14968 || XEXP (rtl, 0) == frame_pointer_rtx)
14969 && SCALAR_INT_MODE_P (mode))
14970 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
14971 INTVAL (XEXP (rtl, 1)),
14972 VAR_INIT_STATUS_INITIALIZED);
14973 else
14974 {
14975 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14976 VAR_INIT_STATUS_INITIALIZED);
14977 if (mem_loc_result == 0)
14978 break;
14979
14980 if (CONST_INT_P (XEXP (rtl, 1))
14981 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
14982 loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
14983 else
14984 {
14985 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
14986 VAR_INIT_STATUS_INITIALIZED);
14987 if (op1 == 0)
14988 return NULL;
14989 add_loc_descr (&mem_loc_result, op1);
14990 add_loc_descr (&mem_loc_result,
14991 new_loc_descr (DW_OP_plus, 0, 0));
14992 }
14993 }
14994 break;
14995
14996 /* If a pseudo-reg is optimized away, it is possible for it to
14997 be replaced with a MEM containing a multiply or shift. */
14998 case MINUS:
14999 op = DW_OP_minus;
15000 goto do_binop;
15001
15002 case MULT:
15003 op = DW_OP_mul;
15004 goto do_binop;
15005
15006 case DIV:
15007 if ((!dwarf_strict || dwarf_version >= 5)
15008 && SCALAR_INT_MODE_P (mode)
15009 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
15010 {
15011 mem_loc_result = typed_binop (DW_OP_div, rtl,
15012 base_type_for_mode (mode, 0),
15013 mode, mem_mode);
15014 break;
15015 }
15016 op = DW_OP_div;
15017 goto do_binop;
15018
15019 case UMOD:
15020 op = DW_OP_mod;
15021 goto do_binop;
15022
15023 case ASHIFT:
15024 op = DW_OP_shl;
15025 goto do_shift;
15026
15027 case ASHIFTRT:
15028 op = DW_OP_shra;
15029 goto do_shift;
15030
15031 case LSHIFTRT:
15032 op = DW_OP_shr;
15033 goto do_shift;
15034
15035 do_shift:
15036 if (!SCALAR_INT_MODE_P (mode))
15037 break;
15038 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15039 VAR_INIT_STATUS_INITIALIZED);
15040 {
15041 rtx rtlop1 = XEXP (rtl, 1);
15042 if (GET_MODE (rtlop1) != VOIDmode
15043 && GET_MODE_BITSIZE (GET_MODE (rtlop1))
15044 < GET_MODE_BITSIZE (mode))
15045 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
15046 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
15047 VAR_INIT_STATUS_INITIALIZED);
15048 }
15049
15050 if (op0 == 0 || op1 == 0)
15051 break;
15052
15053 mem_loc_result = op0;
15054 add_loc_descr (&mem_loc_result, op1);
15055 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15056 break;
15057
15058 case AND:
15059 op = DW_OP_and;
15060 goto do_binop;
15061
15062 case IOR:
15063 op = DW_OP_or;
15064 goto do_binop;
15065
15066 case XOR:
15067 op = DW_OP_xor;
15068 goto do_binop;
15069
15070 do_binop:
15071 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15072 VAR_INIT_STATUS_INITIALIZED);
15073 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15074 VAR_INIT_STATUS_INITIALIZED);
15075
15076 if (op0 == 0 || op1 == 0)
15077 break;
15078
15079 mem_loc_result = op0;
15080 add_loc_descr (&mem_loc_result, op1);
15081 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15082 break;
15083
15084 case MOD:
15085 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
15086 && (!dwarf_strict || dwarf_version >= 5))
15087 {
15088 mem_loc_result = typed_binop (DW_OP_mod, rtl,
15089 base_type_for_mode (mode, 0),
15090 mode, mem_mode);
15091 break;
15092 }
15093
15094 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15095 VAR_INIT_STATUS_INITIALIZED);
15096 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15097 VAR_INIT_STATUS_INITIALIZED);
15098
15099 if (op0 == 0 || op1 == 0)
15100 break;
15101
15102 mem_loc_result = op0;
15103 add_loc_descr (&mem_loc_result, op1);
15104 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
15105 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
15106 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
15107 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
15108 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
15109 break;
15110
15111 case UDIV:
15112 if ((!dwarf_strict || dwarf_version >= 5)
15113 && SCALAR_INT_MODE_P (mode))
15114 {
15115 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
15116 {
15117 op = DW_OP_div;
15118 goto do_binop;
15119 }
15120 mem_loc_result = typed_binop (DW_OP_div, rtl,
15121 base_type_for_mode (mode, 1),
15122 mode, mem_mode);
15123 }
15124 break;
15125
15126 case NOT:
15127 op = DW_OP_not;
15128 goto do_unop;
15129
15130 case ABS:
15131 op = DW_OP_abs;
15132 goto do_unop;
15133
15134 case NEG:
15135 op = DW_OP_neg;
15136 goto do_unop;
15137
15138 do_unop:
15139 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15140 VAR_INIT_STATUS_INITIALIZED);
15141
15142 if (op0 == 0)
15143 break;
15144
15145 mem_loc_result = op0;
15146 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15147 break;
15148
15149 case CONST_INT:
15150 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
15151#ifdef POINTERS_EXTEND_UNSIGNED
15152 || (mode == Pmode
15153 && mem_mode != VOIDmode
15154 && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
15155#endif
15156 )
15157 {
15158 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
15159 break;
15160 }
15161 if ((!dwarf_strict || dwarf_version >= 5)
15162 && (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT
15163 || GET_MODE_BITSIZE (mode) == HOST_BITS_PER_DOUBLE_INT))
15164 {
15165 dw_die_ref type_die = base_type_for_mode (mode, 1);
15166 machine_mode amode;
15167 if (type_die == NULL)
15168 return NULL;
15169 amode = mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT,
15170 MODE_INT, 0);
15171 if (INTVAL (rtl) >= 0
15172 && amode != BLKmode
15173 && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
15174 /* const DW_OP_convert <XXX> vs.
15175 DW_OP_const_type <XXX, 1, const>. */
15176 && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
15177 < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (mode))
15178 {
15179 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
15180 op0 = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15181 op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15182 op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15183 op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
15184 add_loc_descr (&mem_loc_result, op0);
15185 return mem_loc_result;
15186 }
15187 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0,
15188 INTVAL (rtl));
15189 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15190 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15191 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
15192 if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
15193 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
15194 else
15195 {
15196 mem_loc_result->dw_loc_oprnd2.val_class
15197 = dw_val_class_const_double;
15198 mem_loc_result->dw_loc_oprnd2.v.val_double
15199 = double_int::from_shwi (INTVAL (rtl));
15200 }
15201 }
15202 break;
15203
15204 case CONST_DOUBLE:
15205 if (!dwarf_strict || dwarf_version >= 5)
15206 {
15207 dw_die_ref type_die;
15208
15209 /* Note that if TARGET_SUPPORTS_WIDE_INT == 0, a
15210 CONST_DOUBLE rtx could represent either a large integer
15211 or a floating-point constant. If TARGET_SUPPORTS_WIDE_INT != 0,
15212 the value is always a floating point constant.
15213
15214 When it is an integer, a CONST_DOUBLE is used whenever
15215 the constant requires 2 HWIs to be adequately represented.
15216 We output CONST_DOUBLEs as blocks. */
15217 if (mode == VOIDmode
15218 || (GET_MODE (rtl) == VOIDmode
15219 && GET_MODE_BITSIZE (mode) != HOST_BITS_PER_DOUBLE_INT))
15220 break;
15221 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
15222 if (type_die == NULL)
15223 return NULL;
15224 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0, 0);
15225 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15226 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15227 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
15228#if TARGET_SUPPORTS_WIDE_INT == 0
15229 if (!SCALAR_FLOAT_MODE_P (mode))
15230 {
15231 mem_loc_result->dw_loc_oprnd2.val_class
15232 = dw_val_class_const_double;
15233 mem_loc_result->dw_loc_oprnd2.v.val_double
15234 = rtx_to_double_int (rtl);
15235 }
15236 else
15237#endif
15238 {
15239 unsigned int length = GET_MODE_SIZE (mode);
15240 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
15241
15242 insert_float (rtl, array);
15243 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
15244 mem_loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
15245 mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
15246 mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
15247 }
15248 }
15249 break;
15250
15251 case CONST_WIDE_INT:
15252 if (!dwarf_strict || dwarf_version >= 5)
15253 {
15254 dw_die_ref type_die;
15255
15256 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
15257 if (type_die == NULL)
15258 return NULL;
15259 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0, 0);
15260 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15261 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15262 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
15263 mem_loc_result->dw_loc_oprnd2.val_class
15264 = dw_val_class_wide_int;
15265 mem_loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
15266 *mem_loc_result->dw_loc_oprnd2.v.val_wide = rtx_mode_t (rtl, mode);
15267 }
15268 break;
15269
15270 case EQ:
15271 mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
15272 break;
15273
15274 case GE:
15275 mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
15276 break;
15277
15278 case GT:
15279 mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
15280 break;
15281
15282 case LE:
15283 mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
15284 break;
15285
15286 case LT:
15287 mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
15288 break;
15289
15290 case NE:
15291 mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
15292 break;
15293
15294 case GEU:
15295 mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
15296 break;
15297
15298 case GTU:
15299 mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
15300 break;
15301
15302 case LEU:
15303 mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
15304 break;
15305
15306 case LTU:
15307 mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
15308 break;
15309
15310 case UMIN:
15311 case UMAX:
15312 if (!SCALAR_INT_MODE_P (mode))
15313 break;
15314 /* FALLTHRU */
15315 case SMIN:
15316 case SMAX:
15317 mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
15318 break;
15319
15320 case ZERO_EXTRACT:
15321 case SIGN_EXTRACT:
15322 if (CONST_INT_P (XEXP (rtl, 1))
15323 && CONST_INT_P (XEXP (rtl, 2))
15324 && ((unsigned) INTVAL (XEXP (rtl, 1))
15325 + (unsigned) INTVAL (XEXP (rtl, 2))
15326 <= GET_MODE_BITSIZE (mode))
15327 && SCALAR_INT_MODE_P (mode)
15328 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
15329 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
15330 {
15331 int shift, size;
15332 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
15333 mem_mode, VAR_INIT_STATUS_INITIALIZED);
15334 if (op0 == 0)
15335 break;
15336 if (GET_CODE (rtl) == SIGN_EXTRACT)
15337 op = DW_OP_shra;
15338 else
15339 op = DW_OP_shr;
15340 mem_loc_result = op0;
15341 size = INTVAL (XEXP (rtl, 1));
15342 shift = INTVAL (XEXP (rtl, 2));
15343 if (BITS_BIG_ENDIAN)
15344 shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
15345 - shift - size;
15346 if (shift + size != (int) DWARF2_ADDR_SIZE)
15347 {
15348 add_loc_descr (&mem_loc_result,
15349 int_loc_descriptor (DWARF2_ADDR_SIZE
15350 - shift - size));
15351 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
15352 }
15353 if (size != (int) DWARF2_ADDR_SIZE)
15354 {
15355 add_loc_descr (&mem_loc_result,
15356 int_loc_descriptor (DWARF2_ADDR_SIZE - size));
15357 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15358 }
15359 }
15360 break;
15361
15362 case IF_THEN_ELSE:
15363 {
15364 dw_loc_descr_ref op2, bra_node, drop_node;
15365 op0 = mem_loc_descriptor (XEXP (rtl, 0),
15366 GET_MODE (XEXP (rtl, 0)) == VOIDmode
15367 ? word_mode : GET_MODE (XEXP (rtl, 0)),
15368 mem_mode, VAR_INIT_STATUS_INITIALIZED);
15369 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15370 VAR_INIT_STATUS_INITIALIZED);
15371 op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
15372 VAR_INIT_STATUS_INITIALIZED);
15373 if (op0 == NULL || op1 == NULL || op2 == NULL)
15374 break;
15375
15376 mem_loc_result = op1;
15377 add_loc_descr (&mem_loc_result, op2);
15378 add_loc_descr (&mem_loc_result, op0);
15379 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
15380 add_loc_descr (&mem_loc_result, bra_node);
15381 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
15382 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
15383 add_loc_descr (&mem_loc_result, drop_node);
15384 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
15385 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
15386 }
15387 break;
15388
15389 case FLOAT_EXTEND:
15390 case FLOAT_TRUNCATE:
15391 case FLOAT:
15392 case UNSIGNED_FLOAT:
15393 case FIX:
15394 case UNSIGNED_FIX:
15395 if (!dwarf_strict || dwarf_version >= 5)
15396 {
15397 dw_die_ref type_die;
15398 dw_loc_descr_ref cvt;
15399
15400 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
15401 mem_mode, VAR_INIT_STATUS_INITIALIZED);
15402 if (op0 == NULL)
15403 break;
15404 if (SCALAR_INT_MODE_P (GET_MODE (XEXP (rtl, 0)))
15405 && (GET_CODE (rtl) == FLOAT
15406 || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)))
15407 <= DWARF2_ADDR_SIZE))
15408 {
15409 type_die = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
15410 GET_CODE (rtl) == UNSIGNED_FLOAT);
15411 if (type_die == NULL)
15412 break;
15413 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15414 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15415 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15416 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15417 add_loc_descr (&op0, cvt);
15418 }
15419 type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
15420 if (type_die == NULL)
15421 break;
15422 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15423 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15424 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15425 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15426 add_loc_descr (&op0, cvt);
15427 if (SCALAR_INT_MODE_P (mode)
15428 && (GET_CODE (rtl) == FIX
15429 || GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE))
15430 {
15431 op0 = convert_descriptor_to_mode (mode, op0);
15432 if (op0 == NULL)
15433 break;
15434 }
15435 mem_loc_result = op0;
15436 }
15437 break;
15438
15439 case CLZ:
15440 case CTZ:
15441 case FFS:
15442 mem_loc_result = clz_loc_descriptor (rtl, mode, mem_mode);
15443 break;
15444
15445 case POPCOUNT:
15446 case PARITY:
15447 mem_loc_result = popcount_loc_descriptor (rtl, mode, mem_mode);
15448 break;
15449
15450 case BSWAP:
15451 mem_loc_result = bswap_loc_descriptor (rtl, mode, mem_mode);
15452 break;
15453
15454 case ROTATE:
15455 case ROTATERT:
15456 mem_loc_result = rotate_loc_descriptor (rtl, mode, mem_mode);
15457 break;
15458
15459 case COMPARE:
15460 /* In theory, we could implement the above. */
15461 /* DWARF cannot represent the unsigned compare operations
15462 natively. */
15463 case SS_MULT:
15464 case US_MULT:
15465 case SS_DIV:
15466 case US_DIV:
15467 case SS_PLUS:
15468 case US_PLUS:
15469 case SS_MINUS:
15470 case US_MINUS:
15471 case SS_NEG:
15472 case US_NEG:
15473 case SS_ABS:
15474 case SS_ASHIFT:
15475 case US_ASHIFT:
15476 case SS_TRUNCATE:
15477 case US_TRUNCATE:
15478 case UNORDERED:
15479 case ORDERED:
15480 case UNEQ:
15481 case UNGE:
15482 case UNGT:
15483 case UNLE:
15484 case UNLT:
15485 case LTGT:
15486 case FRACT_CONVERT:
15487 case UNSIGNED_FRACT_CONVERT:
15488 case SAT_FRACT:
15489 case UNSIGNED_SAT_FRACT:
15490 case SQRT:
15491 case ASM_OPERANDS:
15492 case VEC_MERGE:
15493 case VEC_SELECT:
15494 case VEC_CONCAT:
15495 case VEC_DUPLICATE:
15496 case UNSPEC:
15497 case HIGH:
15498 case FMA:
15499 case STRICT_LOW_PART:
15500 case CONST_VECTOR:
15501 case CONST_FIXED:
15502 case CLRSB:
15503 case CLOBBER:
15504 /* If delegitimize_address couldn't do anything with the UNSPEC, we
15505 can't express it in the debug info. This can happen e.g. with some
15506 TLS UNSPECs. */
15507 break;
15508
15509 case CONST_STRING:
15510 resolve_one_addr (&rtl);
15511 goto symref;
15512
15513 /* RTL sequences inside PARALLEL record a series of DWARF operations for
15514 the expression. An UNSPEC rtx represents a raw DWARF operation,
15515 new_loc_descr is called for it to build the operation directly.
15516 Otherwise mem_loc_descriptor is called recursively. */
15517 case PARALLEL:
15518 {
15519 int index = 0;
15520 dw_loc_descr_ref exp_result = NULL;
15521
15522 for (; index < XVECLEN (rtl, 0); index++)
15523 {
15524 rtx elem = XVECEXP (rtl, 0, index);
15525 if (GET_CODE (elem) == UNSPEC)
15526 {
15527 /* Each DWARF operation UNSPEC contain two operands, if
15528 one operand is not used for the operation, const0_rtx is
15529 passed. */
15530 gcc_assert (XVECLEN (elem, 0) == 2);
15531
15532 HOST_WIDE_INT dw_op = XINT (elem, 1);
15533 HOST_WIDE_INT oprnd1 = INTVAL (XVECEXP (elem, 0, 0));
15534 HOST_WIDE_INT oprnd2 = INTVAL (XVECEXP (elem, 0, 1));
15535 exp_result
15536 = new_loc_descr ((enum dwarf_location_atom) dw_op, oprnd1,
15537 oprnd2);
15538 }
15539 else
15540 exp_result
15541 = mem_loc_descriptor (elem, mode, mem_mode,
15542 VAR_INIT_STATUS_INITIALIZED);
15543
15544 if (!mem_loc_result)
15545 mem_loc_result = exp_result;
15546 else
15547 add_loc_descr (&mem_loc_result, exp_result);
15548 }
15549
15550 break;
15551 }
15552
15553 default:
15554 if (flag_checking)
15555 {
15556 print_rtl (stderr, rtl);
15557 gcc_unreachable ();
15558 }
15559 break;
15560 }
15561
15562 if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
15563 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
15564
15565 return mem_loc_result;
15566}
15567
15568/* Return a descriptor that describes the concatenation of two locations.
15569 This is typically a complex variable. */
15570
15571static dw_loc_descr_ref
15572concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
15573{
15574 dw_loc_descr_ref cc_loc_result = NULL;
15575 dw_loc_descr_ref x0_ref
15576 = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
15577 dw_loc_descr_ref x1_ref
15578 = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
15579
15580 if (x0_ref == 0 || x1_ref == 0)
15581 return 0;
15582
15583 cc_loc_result = x0_ref;
15584 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
15585
15586 add_loc_descr (&cc_loc_result, x1_ref);
15587 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
15588
15589 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
15590 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
15591
15592 return cc_loc_result;
15593}
15594
15595/* Return a descriptor that describes the concatenation of N
15596 locations. */
15597
15598static dw_loc_descr_ref
15599concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
15600{
15601 unsigned int i;
15602 dw_loc_descr_ref cc_loc_result = NULL;
15603 unsigned int n = XVECLEN (concatn, 0);
15604
15605 for (i = 0; i < n; ++i)
15606 {
15607 dw_loc_descr_ref ref;
15608 rtx x = XVECEXP (concatn, 0, i);
15609
15610 ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
15611 if (ref == NULL)
15612 return NULL;
15613
15614 add_loc_descr (&cc_loc_result, ref);
15615 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
15616 }
15617
15618 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
15619 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
15620
15621 return cc_loc_result;
15622}
15623
15624/* Helper function for loc_descriptor. Return DW_OP_implicit_pointer
15625 for DEBUG_IMPLICIT_PTR RTL. */
15626
15627static dw_loc_descr_ref
15628implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
15629{
15630 dw_loc_descr_ref ret;
15631 dw_die_ref ref;
15632
15633 if (dwarf_strict && dwarf_version < 5)
15634 return NULL;
15635 gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
15636 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
15637 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
15638 ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
15639 ret = new_loc_descr (dwarf_OP (DW_OP_implicit_pointer), 0, offset);
15640 ret->dw_loc_oprnd2.val_class = dw_val_class_const;
15641 if (ref)
15642 {
15643 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15644 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
15645 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
15646 }
15647 else
15648 {
15649 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
15650 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
15651 }
15652 return ret;
15653}
15654
15655/* Output a proper Dwarf location descriptor for a variable or parameter
15656 which is either allocated in a register or in a memory location. For a
15657 register, we just generate an OP_REG and the register number. For a
15658 memory location we provide a Dwarf postfix expression describing how to
15659 generate the (dynamic) address of the object onto the address stack.
15660
15661 MODE is mode of the decl if this loc_descriptor is going to be used in
15662 .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
15663 allowed, VOIDmode otherwise.
15664
15665 If we don't know how to describe it, return 0. */
15666
15667static dw_loc_descr_ref
15668loc_descriptor (rtx rtl, machine_mode mode,
15669 enum var_init_status initialized)
15670{
15671 dw_loc_descr_ref loc_result = NULL;
15672
15673 switch (GET_CODE (rtl))
15674 {
15675 case SUBREG:
15676 /* The case of a subreg may arise when we have a local (register)
15677 variable or a formal (register) parameter which doesn't quite fill
15678 up an entire register. For now, just assume that it is
15679 legitimate to make the Dwarf info refer to the whole register which
15680 contains the given subreg. */
15681 if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
15682 loc_result = loc_descriptor (SUBREG_REG (rtl),
15683 GET_MODE (SUBREG_REG (rtl)), initialized);
15684 else
15685 goto do_default;
15686 break;
15687
15688 case REG:
15689 loc_result = reg_loc_descriptor (rtl, initialized);
15690 break;
15691
15692 case MEM:
15693 loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
15694 GET_MODE (rtl), initialized);
15695 if (loc_result == NULL)
15696 loc_result = tls_mem_loc_descriptor (rtl);
15697 if (loc_result == NULL)
15698 {
15699 rtx new_rtl = avoid_constant_pool_reference (rtl);
15700 if (new_rtl != rtl)
15701 loc_result = loc_descriptor (new_rtl, mode, initialized);
15702 }
15703 break;
15704
15705 case CONCAT:
15706 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
15707 initialized);
15708 break;
15709
15710 case CONCATN:
15711 loc_result = concatn_loc_descriptor (rtl, initialized);
15712 break;
15713
15714 case VAR_LOCATION:
15715 /* Single part. */
15716 if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
15717 {
15718 rtx loc = PAT_VAR_LOCATION_LOC (rtl);
15719 if (GET_CODE (loc) == EXPR_LIST)
15720 loc = XEXP (loc, 0);
15721 loc_result = loc_descriptor (loc, mode, initialized);
15722 break;
15723 }
15724
15725 rtl = XEXP (rtl, 1);
15726 /* FALLTHRU */
15727
15728 case PARALLEL:
15729 {
15730 rtvec par_elems = XVEC (rtl, 0);
15731 int num_elem = GET_NUM_ELEM (par_elems);
15732 machine_mode mode;
15733 int i;
15734
15735 /* Create the first one, so we have something to add to. */
15736 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
15737 VOIDmode, initialized);
15738 if (loc_result == NULL)
15739 return NULL;
15740 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
15741 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
15742 for (i = 1; i < num_elem; i++)
15743 {
15744 dw_loc_descr_ref temp;
15745
15746 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
15747 VOIDmode, initialized);
15748 if (temp == NULL)
15749 return NULL;
15750 add_loc_descr (&loc_result, temp);
15751 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
15752 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
15753 }
15754 }
15755 break;
15756
15757 case CONST_INT:
15758 if (mode != VOIDmode && mode != BLKmode)
15759 loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
15760 INTVAL (rtl));
15761 break;
15762
15763 case CONST_DOUBLE:
15764 if (mode == VOIDmode)
15765 mode = GET_MODE (rtl);
15766
15767 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
15768 {
15769 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
15770
15771 /* Note that a CONST_DOUBLE rtx could represent either an integer
15772 or a floating-point constant. A CONST_DOUBLE is used whenever
15773 the constant requires more than one word in order to be
15774 adequately represented. We output CONST_DOUBLEs as blocks. */
15775 loc_result = new_loc_descr (DW_OP_implicit_value,
15776 GET_MODE_SIZE (mode), 0);
15777#if TARGET_SUPPORTS_WIDE_INT == 0
15778 if (!SCALAR_FLOAT_MODE_P (mode))
15779 {
15780 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
15781 loc_result->dw_loc_oprnd2.v.val_double
15782 = rtx_to_double_int (rtl);
15783 }
15784 else
15785#endif
15786 {
15787 unsigned int length = GET_MODE_SIZE (mode);
15788 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
15789
15790 insert_float (rtl, array);
15791 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
15792 loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
15793 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
15794 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
15795 }
15796 }
15797 break;
15798
15799 case CONST_WIDE_INT:
15800 if (mode == VOIDmode)
15801 mode = GET_MODE (rtl);
15802
15803 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
15804 {
15805 loc_result = new_loc_descr (DW_OP_implicit_value,
15806 GET_MODE_SIZE (mode), 0);
15807 loc_result->dw_loc_oprnd2.val_class = dw_val_class_wide_int;
15808 loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
15809 *loc_result->dw_loc_oprnd2.v.val_wide = rtx_mode_t (rtl, mode);
15810 }
15811 break;
15812
15813 case CONST_VECTOR:
15814 if (mode == VOIDmode)
15815 mode = GET_MODE (rtl);
15816
15817 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
15818 {
15819 unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
15820 unsigned int length = CONST_VECTOR_NUNITS (rtl);
15821 unsigned char *array
15822 = ggc_vec_alloc<unsigned char> (length * elt_size);
15823 unsigned int i;
15824 unsigned char *p;
15825 machine_mode imode = GET_MODE_INNER (mode);
15826
15827 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
15828 switch (GET_MODE_CLASS (mode))
15829 {
15830 case MODE_VECTOR_INT:
15831 for (i = 0, p = array; i < length; i++, p += elt_size)
15832 {
15833 rtx elt = CONST_VECTOR_ELT (rtl, i);
15834 insert_wide_int (rtx_mode_t (elt, imode), p, elt_size);
15835 }
15836 break;
15837
15838 case MODE_VECTOR_FLOAT:
15839 for (i = 0, p = array; i < length; i++, p += elt_size)
15840 {
15841 rtx elt = CONST_VECTOR_ELT (rtl, i);
15842 insert_float (elt, p);
15843 }
15844 break;
15845
15846 default:
15847 gcc_unreachable ();
15848 }
15849
15850 loc_result = new_loc_descr (DW_OP_implicit_value,
15851 length * elt_size, 0);
15852 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
15853 loc_result->dw_loc_oprnd2.v.val_vec.length = length;
15854 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
15855 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
15856 }
15857 break;
15858
15859 case CONST:
15860 if (mode == VOIDmode
15861 || CONST_SCALAR_INT_P (XEXP (rtl, 0))
15862 || CONST_DOUBLE_AS_FLOAT_P (XEXP (rtl, 0))
15863 || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
15864 {
15865 loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
15866 break;
15867 }
15868 /* FALLTHROUGH */
15869 case SYMBOL_REF:
15870 if (!const_ok_for_output (rtl))
15871 break;
15872 /* FALLTHROUGH */
15873 case LABEL_REF:
15874 if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
15875 && (dwarf_version >= 4 || !dwarf_strict))
15876 {
15877 loc_result = new_addr_loc_descr (rtl, dtprel_false);
15878 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
15879 vec_safe_push (used_rtx_array, rtl);
15880 }
15881 break;
15882
15883 case DEBUG_IMPLICIT_PTR:
15884 loc_result = implicit_ptr_descriptor (rtl, 0);
15885 break;
15886
15887 case PLUS:
15888 if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
15889 && CONST_INT_P (XEXP (rtl, 1)))
15890 {
15891 loc_result
15892 = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
15893 break;
15894 }
15895 /* FALLTHRU */
15896 do_default:
15897 default:
15898 if ((SCALAR_INT_MODE_P (mode)
15899 && GET_MODE (rtl) == mode
15900 && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
15901 && dwarf_version >= 4)
15902 || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
15903 {
15904 /* Value expression. */
15905 loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
15906 if (loc_result)
15907 add_loc_descr (&loc_result,
15908 new_loc_descr (DW_OP_stack_value, 0, 0));
15909 }
15910 break;
15911 }
15912
15913 return loc_result;
15914}
15915
15916/* We need to figure out what section we should use as the base for the
15917 address ranges where a given location is valid.
15918 1. If this particular DECL has a section associated with it, use that.
15919 2. If this function has a section associated with it, use that.
15920 3. Otherwise, use the text section.
15921 XXX: If you split a variable across multiple sections, we won't notice. */
15922
15923static const char *
15924secname_for_decl (const_tree decl)
15925{
15926 const char *secname;
15927
15928 if (VAR_OR_FUNCTION_DECL_P (decl)
15929 && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl) || TREE_STATIC (decl))
15930 && DECL_SECTION_NAME (decl))
15931 secname = DECL_SECTION_NAME (decl);
15932 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
15933 secname = DECL_SECTION_NAME (current_function_decl);
15934 else if (cfun && in_cold_section_p)
15935 secname = crtl->subsections.cold_section_label;
15936 else
15937 secname = text_section_label;
15938
15939 return secname;
15940}
15941
15942/* Return true when DECL_BY_REFERENCE is defined and set for DECL. */
15943
15944static bool
15945decl_by_reference_p (tree decl)
15946{
15947 return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
15948 || VAR_P (decl))
15949 && DECL_BY_REFERENCE (decl));
15950}
15951
15952/* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
15953 for VARLOC. */
15954
15955static dw_loc_descr_ref
15956dw_loc_list_1 (tree loc, rtx varloc, int want_address,
15957 enum var_init_status initialized)
15958{
15959 int have_address = 0;
15960 dw_loc_descr_ref descr;
15961 machine_mode mode;
15962
15963 if (want_address != 2)
15964 {
15965 gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
15966 /* Single part. */
15967 if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
15968 {
15969 varloc = PAT_VAR_LOCATION_LOC (varloc);
15970 if (GET_CODE (varloc) == EXPR_LIST)
15971 varloc = XEXP (varloc, 0);
15972 mode = GET_MODE (varloc);
15973 if (MEM_P (varloc))
15974 {
15975 rtx addr = XEXP (varloc, 0);
15976 descr = mem_loc_descriptor (addr, get_address_mode (varloc),
15977 mode, initialized);
15978 if (descr)
15979 have_address = 1;
15980 else
15981 {
15982 rtx x = avoid_constant_pool_reference (varloc);
15983 if (x != varloc)
15984 descr = mem_loc_descriptor (x, mode, VOIDmode,
15985 initialized);
15986 }
15987 }
15988 else
15989 descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
15990 }
15991 else
15992 return 0;
15993 }
15994 else
15995 {
15996 if (GET_CODE (varloc) == VAR_LOCATION)
15997 mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
15998 else
15999 mode = DECL_MODE (loc);
16000 descr = loc_descriptor (varloc, mode, initialized);
16001 have_address = 1;
16002 }
16003
16004 if (!descr)
16005 return 0;
16006
16007 if (want_address == 2 && !have_address
16008 && (dwarf_version >= 4 || !dwarf_strict))
16009 {
16010 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
16011 {
16012 expansion_failed (loc, NULL_RTX,
16013 "DWARF address size mismatch");
16014 return 0;
16015 }
16016 add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
16017 have_address = 1;
16018 }
16019 /* Show if we can't fill the request for an address. */
16020 if (want_address && !have_address)
16021 {
16022 expansion_failed (loc, NULL_RTX,
16023 "Want address and only have value");
16024 return 0;
16025 }
16026
16027 /* If we've got an address and don't want one, dereference. */
16028 if (!want_address && have_address)
16029 {
16030 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
16031 enum dwarf_location_atom op;
16032
16033 if (size > DWARF2_ADDR_SIZE || size == -1)
16034 {
16035 expansion_failed (loc, NULL_RTX,
16036 "DWARF address size mismatch");
16037 return 0;
16038 }
16039 else if (size == DWARF2_ADDR_SIZE)
16040 op = DW_OP_deref;
16041 else
16042 op = DW_OP_deref_size;
16043
16044 add_loc_descr (&descr, new_loc_descr (op, size, 0));
16045 }
16046
16047 return descr;
16048}
16049
16050/* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
16051 if it is not possible. */
16052
16053static dw_loc_descr_ref
16054new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
16055{
16056 if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
16057 return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
16058 else if (dwarf_version >= 3 || !dwarf_strict)
16059 return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
16060 else
16061 return NULL;
16062}
16063
16064/* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
16065 for VAR_LOC_NOTE for variable DECL that has been optimized by SRA. */
16066
16067static dw_loc_descr_ref
16068dw_sra_loc_expr (tree decl, rtx loc)
16069{
16070 rtx p;
16071 unsigned HOST_WIDE_INT padsize = 0;
16072 dw_loc_descr_ref descr, *descr_tail;
16073 unsigned HOST_WIDE_INT decl_size;
16074 rtx varloc;
16075 enum var_init_status initialized;
16076
16077 if (DECL_SIZE (decl) == NULL
16078 || !tree_fits_uhwi_p (DECL_SIZE (decl)))
16079 return NULL;
16080
16081 decl_size = tree_to_uhwi (DECL_SIZE (decl));
16082 descr = NULL;
16083 descr_tail = &descr;
16084
16085 for (p = loc; p; p = XEXP (p, 1))
16086 {
16087 unsigned HOST_WIDE_INT bitsize = decl_piece_bitsize (p);
16088 rtx loc_note = *decl_piece_varloc_ptr (p);
16089 dw_loc_descr_ref cur_descr;
16090 dw_loc_descr_ref *tail, last = NULL;
16091 unsigned HOST_WIDE_INT opsize = 0;
16092
16093 if (loc_note == NULL_RTX
16094 || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
16095 {
16096 padsize += bitsize;
16097 continue;
16098 }
16099 initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
16100 varloc = NOTE_VAR_LOCATION (loc_note);
16101 cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
16102 if (cur_descr == NULL)
16103 {
16104 padsize += bitsize;
16105 continue;
16106 }
16107
16108 /* Check that cur_descr either doesn't use
16109 DW_OP_*piece operations, or their sum is equal
16110 to bitsize. Otherwise we can't embed it. */
16111 for (tail = &cur_descr; *tail != NULL;
16112 tail = &(*tail)->dw_loc_next)
16113 if ((*tail)->dw_loc_opc == DW_OP_piece)
16114 {
16115 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
16116 * BITS_PER_UNIT;
16117 last = *tail;
16118 }
16119 else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
16120 {
16121 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
16122 last = *tail;
16123 }
16124
16125 if (last != NULL && opsize != bitsize)
16126 {
16127 padsize += bitsize;
16128 /* Discard the current piece of the descriptor and release any
16129 addr_table entries it uses. */
16130 remove_loc_list_addr_table_entries (cur_descr);
16131 continue;
16132 }
16133
16134 /* If there is a hole, add DW_OP_*piece after empty DWARF
16135 expression, which means that those bits are optimized out. */
16136 if (padsize)
16137 {
16138 if (padsize > decl_size)
16139 {
16140 remove_loc_list_addr_table_entries (cur_descr);
16141 goto discard_descr;
16142 }
16143 decl_size -= padsize;
16144 *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
16145 if (*descr_tail == NULL)
16146 {
16147 remove_loc_list_addr_table_entries (cur_descr);
16148 goto discard_descr;
16149 }
16150 descr_tail = &(*descr_tail)->dw_loc_next;
16151 padsize = 0;
16152 }
16153 *descr_tail = cur_descr;
16154 descr_tail = tail;
16155 if (bitsize > decl_size)
16156 goto discard_descr;
16157 decl_size -= bitsize;
16158 if (last == NULL)
16159 {
16160 HOST_WIDE_INT offset = 0;
16161 if (GET_CODE (varloc) == VAR_LOCATION
16162 && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
16163 {
16164 varloc = PAT_VAR_LOCATION_LOC (varloc);
16165 if (GET_CODE (varloc) == EXPR_LIST)
16166 varloc = XEXP (varloc, 0);
16167 }
16168 do
16169 {
16170 if (GET_CODE (varloc) == CONST
16171 || GET_CODE (varloc) == SIGN_EXTEND
16172 || GET_CODE (varloc) == ZERO_EXTEND)
16173 varloc = XEXP (varloc, 0);
16174 else if (GET_CODE (varloc) == SUBREG)
16175 varloc = SUBREG_REG (varloc);
16176 else
16177 break;
16178 }
16179 while (1);
16180 /* DW_OP_bit_size offset should be zero for register
16181 or implicit location descriptions and empty location
16182 descriptions, but for memory addresses needs big endian
16183 adjustment. */
16184 if (MEM_P (varloc))
16185 {
16186 unsigned HOST_WIDE_INT memsize
16187 = MEM_SIZE (varloc) * BITS_PER_UNIT;
16188 if (memsize != bitsize)
16189 {
16190 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
16191 && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
16192 goto discard_descr;
16193 if (memsize < bitsize)
16194 goto discard_descr;
16195 if (BITS_BIG_ENDIAN)
16196 offset = memsize - bitsize;
16197 }
16198 }
16199
16200 *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
16201 if (*descr_tail == NULL)
16202 goto discard_descr;
16203 descr_tail = &(*descr_tail)->dw_loc_next;
16204 }
16205 }
16206
16207 /* If there were any non-empty expressions, add padding till the end of
16208 the decl. */
16209 if (descr != NULL && decl_size != 0)
16210 {
16211 *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
16212 if (*descr_tail == NULL)
16213 goto discard_descr;
16214 }
16215 return descr;
16216
16217discard_descr:
16218 /* Discard the descriptor and release any addr_table entries it uses. */
16219 remove_loc_list_addr_table_entries (descr);
16220 return NULL;
16221}
16222
16223/* Return the dwarf representation of the location list LOC_LIST of
16224 DECL. WANT_ADDRESS has the same meaning as in loc_list_from_tree
16225 function. */
16226
16227static dw_loc_list_ref
16228dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
16229{
16230 const char *endname, *secname;
16231 rtx varloc;
16232 enum var_init_status initialized;
16233 struct var_loc_node *node;
16234 dw_loc_descr_ref descr;
16235 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
16236 dw_loc_list_ref list = NULL;
16237 dw_loc_list_ref *listp = &list;
16238
16239 /* Now that we know what section we are using for a base,
16240 actually construct the list of locations.
16241 The first location information is what is passed to the
16242 function that creates the location list, and the remaining
16243 locations just get added on to that list.
16244 Note that we only know the start address for a location
16245 (IE location changes), so to build the range, we use
16246 the range [current location start, next location start].
16247 This means we have to special case the last node, and generate
16248 a range of [last location start, end of function label]. */
16249
16250 secname = secname_for_decl (decl);
16251
16252 for (node = loc_list->first; node; node = node->next)
16253 if (GET_CODE (node->loc) == EXPR_LIST
16254 || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
16255 {
16256 if (GET_CODE (node->loc) == EXPR_LIST)
16257 {
16258 /* This requires DW_OP_{,bit_}piece, which is not usable
16259 inside DWARF expressions. */
16260 if (want_address != 2)
16261 continue;
16262 descr = dw_sra_loc_expr (decl, node->loc);
16263 if (descr == NULL)
16264 continue;
16265 }
16266 else
16267 {
16268 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
16269 varloc = NOTE_VAR_LOCATION (node->loc);
16270 descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
16271 }
16272 if (descr)
16273 {
16274 bool range_across_switch = false;
16275 /* If section switch happens in between node->label
16276 and node->next->label (or end of function) and
16277 we can't emit it as a single entry list,
16278 emit two ranges, first one ending at the end
16279 of first partition and second one starting at the
16280 beginning of second partition. */
16281 if (node == loc_list->last_before_switch
16282 && (node != loc_list->first || loc_list->first->next)
16283 && current_function_decl)
16284 {
16285 endname = cfun->fde->dw_fde_end;
16286 range_across_switch = true;
16287 }
16288 /* The variable has a location between NODE->LABEL and
16289 NODE->NEXT->LABEL. */
16290 else if (node->next)
16291 endname = node->next->label;
16292 /* If the variable has a location at the last label
16293 it keeps its location until the end of function. */
16294 else if (!current_function_decl)
16295 endname = text_end_label;
16296 else
16297 {
16298 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
16299 current_function_funcdef_no);
16300 endname = ggc_strdup (label_id);
16301 }
16302
16303 *listp = new_loc_list (descr, node->label, endname, secname);
16304 if (TREE_CODE (decl) == PARM_DECL
16305 && node == loc_list->first
16306 && NOTE_P (node->loc)
16307 && strcmp (node->label, endname) == 0)
16308 (*listp)->force = true;
16309 listp = &(*listp)->dw_loc_next;
16310
16311 if (range_across_switch)
16312 {
16313 if (GET_CODE (node->loc) == EXPR_LIST)
16314 descr = dw_sra_loc_expr (decl, node->loc);
16315 else
16316 {
16317 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
16318 varloc = NOTE_VAR_LOCATION (node->loc);
16319 descr = dw_loc_list_1 (decl, varloc, want_address,
16320 initialized);
16321 }
16322 gcc_assert (descr);
16323 /* The variable has a location between NODE->LABEL and
16324 NODE->NEXT->LABEL. */
16325 if (node->next)
16326 endname = node->next->label;
16327 else
16328 endname = cfun->fde->dw_fde_second_end;
16329 *listp = new_loc_list (descr,
16330 cfun->fde->dw_fde_second_begin,
16331 endname, secname);
16332 listp = &(*listp)->dw_loc_next;
16333 }
16334 }
16335 }
16336
16337 /* Try to avoid the overhead of a location list emitting a location
16338 expression instead, but only if we didn't have more than one
16339 location entry in the first place. If some entries were not
16340 representable, we don't want to pretend a single entry that was
16341 applies to the entire scope in which the variable is
16342 available. */
16343 if (list && loc_list->first->next)
16344 gen_llsym (list);
16345
16346 return list;
16347}
16348
16349/* Return if the loc_list has only single element and thus can be represented
16350 as location description. */
16351
16352static bool
16353single_element_loc_list_p (dw_loc_list_ref list)
16354{
16355 gcc_assert (!list->dw_loc_next || list->ll_symbol);
16356 return !list->ll_symbol;
16357}
16358
16359/* Duplicate a single element of location list. */
16360
16361static inline dw_loc_descr_ref
16362copy_loc_descr (dw_loc_descr_ref ref)
16363{
16364 dw_loc_descr_ref copy = ggc_alloc<dw_loc_descr_node> ();
16365 memcpy (copy, ref, sizeof (dw_loc_descr_node));
16366 return copy;
16367}
16368
16369/* To each location in list LIST append loc descr REF. */
16370
16371static void
16372add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
16373{
16374 dw_loc_descr_ref copy;
16375 add_loc_descr (&list->expr, ref);
16376 list = list->dw_loc_next;
16377 while (list)
16378 {
16379 copy = copy_loc_descr (ref);
16380 add_loc_descr (&list->expr, copy);
16381 while (copy->dw_loc_next)
16382 copy = copy->dw_loc_next = copy_loc_descr (copy->dw_loc_next);
16383 list = list->dw_loc_next;
16384 }
16385}
16386
16387/* To each location in list LIST prepend loc descr REF. */
16388
16389static void
16390prepend_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
16391{
16392 dw_loc_descr_ref copy;
16393 dw_loc_descr_ref ref_end = list->expr;
16394 add_loc_descr (&ref, list->expr);
16395 list->expr = ref;
16396 list = list->dw_loc_next;
16397 while (list)
16398 {
16399 dw_loc_descr_ref end = list->expr;
16400 list->expr = copy = copy_loc_descr (ref);
16401 while (copy->dw_loc_next != ref_end)
16402 copy = copy->dw_loc_next = copy_loc_descr (copy->dw_loc_next);
16403 copy->dw_loc_next = end;
16404 list = list->dw_loc_next;
16405 }
16406}
16407
16408/* Given two lists RET and LIST
16409 produce location list that is result of adding expression in LIST
16410 to expression in RET on each position in program.
16411 Might be destructive on both RET and LIST.
16412
16413 TODO: We handle only simple cases of RET or LIST having at most one
16414 element. General case would involve sorting the lists in program order
16415 and merging them that will need some additional work.
16416 Adding that will improve quality of debug info especially for SRA-ed
16417 structures. */
16418
16419static void
16420add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
16421{
16422 if (!list)
16423 return;
16424 if (!*ret)
16425 {
16426 *ret = list;
16427 return;
16428 }
16429 if (!list->dw_loc_next)
16430 {
16431 add_loc_descr_to_each (*ret, list->expr);
16432 return;
16433 }
16434 if (!(*ret)->dw_loc_next)
16435 {
16436 prepend_loc_descr_to_each (list, (*ret)->expr);
16437 *ret = list;
16438 return;
16439 }
16440 expansion_failed (NULL_TREE, NULL_RTX,
16441 "Don't know how to merge two non-trivial"
16442 " location lists.\n");
16443 *ret = NULL;
16444 return;
16445}
16446
16447/* LOC is constant expression. Try a luck, look it up in constant
16448 pool and return its loc_descr of its address. */
16449
16450static dw_loc_descr_ref
16451cst_pool_loc_descr (tree loc)
16452{
16453 /* Get an RTL for this, if something has been emitted. */
16454 rtx rtl = lookup_constant_def (loc);
16455
16456 if (!rtl || !MEM_P (rtl))
16457 {
16458 gcc_assert (!rtl);
16459 return 0;
16460 }
16461 gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
16462
16463 /* TODO: We might get more coverage if we was actually delaying expansion
16464 of all expressions till end of compilation when constant pools are fully
16465 populated. */
16466 if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
16467 {
16468 expansion_failed (loc, NULL_RTX,
16469 "CST value in contant pool but not marked.");
16470 return 0;
16471 }
16472 return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
16473 GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
16474}
16475
16476/* Return dw_loc_list representing address of addr_expr LOC
16477 by looking for inner INDIRECT_REF expression and turning
16478 it into simple arithmetics.
16479
16480 See loc_list_from_tree for the meaning of CONTEXT. */
16481
16482static dw_loc_list_ref
16483loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev,
16484 loc_descr_context *context)
16485{
16486 tree obj, offset;
16487 HOST_WIDE_INT bitsize, bitpos, bytepos;
16488 machine_mode mode;
16489 int unsignedp, reversep, volatilep = 0;
16490 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
16491
16492 obj = get_inner_reference (TREE_OPERAND (loc, 0),
16493 &bitsize, &bitpos, &offset, &mode,
16494 &unsignedp, &reversep, &volatilep);
16495 STRIP_NOPS (obj);
16496 if (bitpos % BITS_PER_UNIT)
16497 {
16498 expansion_failed (loc, NULL_RTX, "bitfield access");
16499 return 0;
16500 }
16501 if (!INDIRECT_REF_P (obj))
16502 {
16503 expansion_failed (obj,
16504 NULL_RTX, "no indirect ref in inner refrence");
16505 return 0;
16506 }
16507 if (!offset && !bitpos)
16508 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1,
16509 context);
16510 else if (toplev
16511 && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
16512 && (dwarf_version >= 4 || !dwarf_strict))
16513 {
16514 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0, context);
16515 if (!list_ret)
16516 return 0;
16517 if (offset)
16518 {
16519 /* Variable offset. */
16520 list_ret1 = loc_list_from_tree (offset, 0, context);
16521 if (list_ret1 == 0)
16522 return 0;
16523 add_loc_list (&list_ret, list_ret1);
16524 if (!list_ret)
16525 return 0;
16526 add_loc_descr_to_each (list_ret,
16527 new_loc_descr (DW_OP_plus, 0, 0));
16528 }
16529 bytepos = bitpos / BITS_PER_UNIT;
16530 if (bytepos > 0)
16531 add_loc_descr_to_each (list_ret,
16532 new_loc_descr (DW_OP_plus_uconst,
16533 bytepos, 0));
16534 else if (bytepos < 0)
16535 loc_list_plus_const (list_ret, bytepos);
16536 add_loc_descr_to_each (list_ret,
16537 new_loc_descr (DW_OP_stack_value, 0, 0));
16538 }
16539 return list_ret;
16540}
16541
16542/* Set LOC to the next operation that is not a DW_OP_nop operation. In the case
16543 all operations from LOC are nops, move to the last one. Insert in NOPS all
16544 operations that are skipped. */
16545
16546static void
16547loc_descr_to_next_no_nop (dw_loc_descr_ref &loc,
16548 hash_set<dw_loc_descr_ref> &nops)
16549{
16550 while (loc->dw_loc_next != NULL && loc->dw_loc_opc == DW_OP_nop)
16551 {
16552 nops.add (loc);
16553 loc = loc->dw_loc_next;
16554 }
16555}
16556
16557/* Helper for loc_descr_without_nops: free the location description operation
16558 P. */
16559
16560bool
16561free_loc_descr (const dw_loc_descr_ref &loc, void *data ATTRIBUTE_UNUSED)
16562{
16563 ggc_free (loc);
16564 return true;
16565}
16566
16567/* Remove all DW_OP_nop operations from LOC except, if it exists, the one that
16568 finishes LOC. */
16569
16570static void
16571loc_descr_without_nops (dw_loc_descr_ref &loc)
16572{
16573 if (loc->dw_loc_opc == DW_OP_nop && loc->dw_loc_next == NULL)
16574 return;
16575
16576 /* Set of all DW_OP_nop operations we remove. */
16577 hash_set<dw_loc_descr_ref> nops;
16578
16579 /* First, strip all prefix NOP operations in order to keep the head of the
16580 operations list. */
16581 loc_descr_to_next_no_nop (loc, nops);
16582
16583 for (dw_loc_descr_ref cur = loc; cur != NULL;)
16584 {
16585 /* For control flow operations: strip "prefix" nops in destination
16586 labels. */
16587 if (cur->dw_loc_oprnd1.val_class == dw_val_class_loc)
16588 loc_descr_to_next_no_nop (cur->dw_loc_oprnd1.v.val_loc, nops);
16589 if (cur->dw_loc_oprnd2.val_class == dw_val_class_loc)
16590 loc_descr_to_next_no_nop (cur->dw_loc_oprnd2.v.val_loc, nops);
16591
16592 /* Do the same for the operations that follow, then move to the next
16593 iteration. */
16594 if (cur->dw_loc_next != NULL)
16595 loc_descr_to_next_no_nop (cur->dw_loc_next, nops);
16596 cur = cur->dw_loc_next;
16597 }
16598
16599 nops.traverse<void *, free_loc_descr> (NULL);
16600}
16601
16602
16603struct dwarf_procedure_info;
16604
16605/* Helper structure for location descriptions generation. */
16606struct loc_descr_context
16607{
16608 /* The type that is implicitly referenced by DW_OP_push_object_address, or
16609 NULL_TREE if DW_OP_push_object_address in invalid for this location
16610 description. This is used when processing PLACEHOLDER_EXPR nodes. */
16611 tree context_type;
16612 /* The ..._DECL node that should be translated as a
16613 DW_OP_push_object_address operation. */
16614 tree base_decl;
16615 /* Information about the DWARF procedure we are currently generating. NULL if
16616 we are not generating a DWARF procedure. */
16617 struct dwarf_procedure_info *dpi;
16618 /* True if integral PLACEHOLDER_EXPR stands for the first argument passed
16619 by consumer. Used for DW_TAG_generic_subrange attributes. */
16620 bool placeholder_arg;
16621 /* True if PLACEHOLDER_EXPR has been seen. */
16622 bool placeholder_seen;
16623};
16624
16625/* DWARF procedures generation
16626
16627 DWARF expressions (aka. location descriptions) are used to encode variable
16628 things such as sizes or offsets. Such computations can have redundant parts
16629 that can be factorized in order to reduce the size of the output debug
16630 information. This is the whole point of DWARF procedures.
16631
16632 Thanks to stor-layout.c, size and offset expressions in GENERIC trees are
16633 already factorized into functions ("size functions") in order to handle very
16634 big and complex types. Such functions are quite simple: they have integral
16635 arguments, they return an integral result and their body contains only a
16636 return statement with arithmetic expressions. This is the only kind of
16637 function we are interested in translating into DWARF procedures, here.
16638
16639 DWARF expressions and DWARF procedure are executed using a stack, so we have
16640 to define some calling convention for them to interact. Let's say that:
16641
16642 - Before calling a DWARF procedure, DWARF expressions must push on the stack
16643 all arguments in reverse order (right-to-left) so that when the DWARF
16644 procedure execution starts, the first argument is the top of the stack.
16645
16646 - Then, when returning, the DWARF procedure must have consumed all arguments
16647 on the stack, must have pushed the result and touched nothing else.
16648
16649 - Each integral argument and the result are integral types can be hold in a
16650 single stack slot.
16651
16652 - We call "frame offset" the number of stack slots that are "under DWARF
16653 procedure control": it includes the arguments slots, the temporaries and
16654 the result slot. Thus, it is equal to the number of arguments when the
16655 procedure execution starts and must be equal to one (the result) when it
16656 returns. */
16657
16658/* Helper structure used when generating operations for a DWARF procedure. */
16659struct dwarf_procedure_info
16660{
16661 /* The FUNCTION_DECL node corresponding to the DWARF procedure that is
16662 currently translated. */
16663 tree fndecl;
16664 /* The number of arguments FNDECL takes. */
16665 unsigned args_count;
16666};
16667
16668/* Return a pointer to a newly created DIE node for a DWARF procedure. Add
16669 LOCATION as its DW_AT_location attribute. If FNDECL is not NULL_TREE,
16670 equate it to this DIE. */
16671
16672static dw_die_ref
16673new_dwarf_proc_die (dw_loc_descr_ref location, tree fndecl,
16674 dw_die_ref parent_die)
16675{
16676 dw_die_ref dwarf_proc_die;
16677
16678 if ((dwarf_version < 3 && dwarf_strict)
16679 || location == NULL)
16680 return NULL;
16681
16682 dwarf_proc_die = new_die (DW_TAG_dwarf_procedure, parent_die, fndecl);
16683 if (fndecl)
16684 equate_decl_number_to_die (fndecl, dwarf_proc_die);
16685 add_AT_loc (dwarf_proc_die, DW_AT_location, location);
16686 return dwarf_proc_die;
16687}
16688
16689/* Return whether TYPE is a supported type as a DWARF procedure argument
16690 type or return type (we handle only scalar types and pointer types that
16691 aren't wider than the DWARF expression evaluation stack. */
16692
16693static bool
16694is_handled_procedure_type (tree type)
16695{
16696 return ((INTEGRAL_TYPE_P (type)
16697 || TREE_CODE (type) == OFFSET_TYPE
16698 || TREE_CODE (type) == POINTER_TYPE)
16699 && int_size_in_bytes (type) <= DWARF2_ADDR_SIZE);
16700}
16701
16702/* Helper for resolve_args_picking: do the same but stop when coming across
16703 visited nodes. For each node we visit, register in FRAME_OFFSETS the frame
16704 offset *before* evaluating the corresponding operation. */
16705
16706static bool
16707resolve_args_picking_1 (dw_loc_descr_ref loc, unsigned initial_frame_offset,
16708 struct dwarf_procedure_info *dpi,
16709 hash_map<dw_loc_descr_ref, unsigned> &frame_offsets)
16710{
16711 /* The "frame_offset" identifier is already used to name a macro... */
16712 unsigned frame_offset_ = initial_frame_offset;
16713 dw_loc_descr_ref l;
16714
16715 for (l = loc; l != NULL;)
16716 {
16717 bool existed;
16718 unsigned &l_frame_offset = frame_offsets.get_or_insert (l, &existed);
16719
16720 /* If we already met this node, there is nothing to compute anymore. */
16721 if (existed)
16722 {
16723 /* Make sure that the stack size is consistent wherever the execution
16724 flow comes from. */
16725 gcc_assert ((unsigned) l_frame_offset == frame_offset_);
16726 break;
16727 }
16728 l_frame_offset = frame_offset_;
16729
16730 /* If needed, relocate the picking offset with respect to the frame
16731 offset. */
16732 if (l->frame_offset_rel)
16733 {
16734 unsigned HOST_WIDE_INT off;
16735 switch (l->dw_loc_opc)
16736 {
16737 case DW_OP_pick:
16738 off = l->dw_loc_oprnd1.v.val_unsigned;
16739 break;
16740 case DW_OP_dup:
16741 off = 0;
16742 break;
16743 case DW_OP_over:
16744 off = 1;
16745 break;
16746 default:
16747 gcc_unreachable ();
16748 }
16749 /* frame_offset_ is the size of the current stack frame, including
16750 incoming arguments. Besides, the arguments are pushed
16751 right-to-left. Thus, in order to access the Nth argument from
16752 this operation node, the picking has to skip temporaries *plus*
16753 one stack slot per argument (0 for the first one, 1 for the second
16754 one, etc.).
16755
16756 The targetted argument number (N) is already set as the operand,
16757 and the number of temporaries can be computed with:
16758 frame_offsets_ - dpi->args_count */
16759 off += frame_offset_ - dpi->args_count;
16760
16761 /* DW_OP_pick handles only offsets from 0 to 255 (inclusive)... */
16762 if (off > 255)
16763 return false;
16764
16765 if (off == 0)
16766 {
16767 l->dw_loc_opc = DW_OP_dup;
16768 l->dw_loc_oprnd1.v.val_unsigned = 0;
16769 }
16770 else if (off == 1)
16771 {
16772 l->dw_loc_opc = DW_OP_over;
16773 l->dw_loc_oprnd1.v.val_unsigned = 0;
16774 }
16775 else
16776 {
16777 l->dw_loc_opc = DW_OP_pick;
16778 l->dw_loc_oprnd1.v.val_unsigned = off;
16779 }
16780 }
16781
16782 /* Update frame_offset according to the effect the current operation has
16783 on the stack. */
16784 switch (l->dw_loc_opc)
16785 {
16786 case DW_OP_deref:
16787 case DW_OP_swap:
16788 case DW_OP_rot:
16789 case DW_OP_abs:
16790 case DW_OP_neg:
16791 case DW_OP_not:
16792 case DW_OP_plus_uconst:
16793 case DW_OP_skip:
16794 case DW_OP_reg0:
16795 case DW_OP_reg1:
16796 case DW_OP_reg2:
16797 case DW_OP_reg3:
16798 case DW_OP_reg4:
16799 case DW_OP_reg5:
16800 case DW_OP_reg6:
16801 case DW_OP_reg7:
16802 case DW_OP_reg8:
16803 case DW_OP_reg9:
16804 case DW_OP_reg10:
16805 case DW_OP_reg11:
16806 case DW_OP_reg12:
16807 case DW_OP_reg13:
16808 case DW_OP_reg14:
16809 case DW_OP_reg15:
16810 case DW_OP_reg16:
16811 case DW_OP_reg17:
16812 case DW_OP_reg18:
16813 case DW_OP_reg19:
16814 case DW_OP_reg20:
16815 case DW_OP_reg21:
16816 case DW_OP_reg22:
16817 case DW_OP_reg23:
16818 case DW_OP_reg24:
16819 case DW_OP_reg25:
16820 case DW_OP_reg26:
16821 case DW_OP_reg27:
16822 case DW_OP_reg28:
16823 case DW_OP_reg29:
16824 case DW_OP_reg30:
16825 case DW_OP_reg31:
16826 case DW_OP_bregx:
16827 case DW_OP_piece:
16828 case DW_OP_deref_size:
16829 case DW_OP_nop:
16830 case DW_OP_bit_piece:
16831 case DW_OP_implicit_value:
16832 case DW_OP_stack_value:
16833 break;
16834
16835 case DW_OP_addr:
16836 case DW_OP_const1u:
16837 case DW_OP_const1s:
16838 case DW_OP_const2u:
16839 case DW_OP_const2s:
16840 case DW_OP_const4u:
16841 case DW_OP_const4s:
16842 case DW_OP_const8u:
16843 case DW_OP_const8s:
16844 case DW_OP_constu:
16845 case DW_OP_consts:
16846 case DW_OP_dup:
16847 case DW_OP_over:
16848 case DW_OP_pick:
16849 case DW_OP_lit0:
16850 case DW_OP_lit1:
16851 case DW_OP_lit2:
16852 case DW_OP_lit3:
16853 case DW_OP_lit4:
16854 case DW_OP_lit5:
16855 case DW_OP_lit6:
16856 case DW_OP_lit7:
16857 case DW_OP_lit8:
16858 case DW_OP_lit9:
16859 case DW_OP_lit10:
16860 case DW_OP_lit11:
16861 case DW_OP_lit12:
16862 case DW_OP_lit13:
16863 case DW_OP_lit14:
16864 case DW_OP_lit15:
16865 case DW_OP_lit16:
16866 case DW_OP_lit17:
16867 case DW_OP_lit18:
16868 case DW_OP_lit19:
16869 case DW_OP_lit20:
16870 case DW_OP_lit21:
16871 case DW_OP_lit22:
16872 case DW_OP_lit23:
16873 case DW_OP_lit24:
16874 case DW_OP_lit25:
16875 case DW_OP_lit26:
16876 case DW_OP_lit27:
16877 case DW_OP_lit28:
16878 case DW_OP_lit29:
16879 case DW_OP_lit30:
16880 case DW_OP_lit31:
16881 case DW_OP_breg0:
16882 case DW_OP_breg1:
16883 case DW_OP_breg2:
16884 case DW_OP_breg3:
16885 case DW_OP_breg4:
16886 case DW_OP_breg5:
16887 case DW_OP_breg6:
16888 case DW_OP_breg7:
16889 case DW_OP_breg8:
16890 case DW_OP_breg9:
16891 case DW_OP_breg10:
16892 case DW_OP_breg11:
16893 case DW_OP_breg12:
16894 case DW_OP_breg13:
16895 case DW_OP_breg14:
16896 case DW_OP_breg15:
16897 case DW_OP_breg16:
16898 case DW_OP_breg17:
16899 case DW_OP_breg18:
16900 case DW_OP_breg19:
16901 case DW_OP_breg20:
16902 case DW_OP_breg21:
16903 case DW_OP_breg22:
16904 case DW_OP_breg23:
16905 case DW_OP_breg24:
16906 case DW_OP_breg25:
16907 case DW_OP_breg26:
16908 case DW_OP_breg27:
16909 case DW_OP_breg28:
16910 case DW_OP_breg29:
16911 case DW_OP_breg30:
16912 case DW_OP_breg31:
16913 case DW_OP_fbreg:
16914 case DW_OP_push_object_address:
16915 case DW_OP_call_frame_cfa:
16916 case DW_OP_GNU_variable_value:
16917 ++frame_offset_;
16918 break;
16919
16920 case DW_OP_drop:
16921 case DW_OP_xderef:
16922 case DW_OP_and:
16923 case DW_OP_div:
16924 case DW_OP_minus:
16925 case DW_OP_mod:
16926 case DW_OP_mul:
16927 case DW_OP_or:
16928 case DW_OP_plus:
16929 case DW_OP_shl:
16930 case DW_OP_shr:
16931 case DW_OP_shra:
16932 case DW_OP_xor:
16933 case DW_OP_bra:
16934 case DW_OP_eq:
16935 case DW_OP_ge:
16936 case DW_OP_gt:
16937 case DW_OP_le:
16938 case DW_OP_lt:
16939 case DW_OP_ne:
16940 case DW_OP_regx:
16941 case DW_OP_xderef_size:
16942 --frame_offset_;
16943 break;
16944
16945 case DW_OP_call2:
16946 case DW_OP_call4:
16947 case DW_OP_call_ref:
16948 {
16949 dw_die_ref dwarf_proc = l->dw_loc_oprnd1.v.val_die_ref.die;
16950 int *stack_usage = dwarf_proc_stack_usage_map->get (dwarf_proc);
16951
16952 if (stack_usage == NULL)
16953 return false;
16954 frame_offset_ += *stack_usage;
16955 break;
16956 }
16957
16958 case DW_OP_implicit_pointer:
16959 case DW_OP_entry_value:
16960 case DW_OP_const_type:
16961 case DW_OP_regval_type:
16962 case DW_OP_deref_type:
16963 case DW_OP_convert:
16964 case DW_OP_reinterpret:
16965 case DW_OP_form_tls_address:
16966 case DW_OP_GNU_push_tls_address:
16967 case DW_OP_GNU_uninit:
16968 case DW_OP_GNU_encoded_addr:
16969 case DW_OP_GNU_implicit_pointer:
16970 case DW_OP_GNU_entry_value:
16971 case DW_OP_GNU_const_type:
16972 case DW_OP_GNU_regval_type:
16973 case DW_OP_GNU_deref_type:
16974 case DW_OP_GNU_convert:
16975 case DW_OP_GNU_reinterpret:
16976 case DW_OP_GNU_parameter_ref:
16977 /* loc_list_from_tree will probably not output these operations for
16978 size functions, so assume they will not appear here. */
16979 /* Fall through... */
16980
16981 default:
16982 gcc_unreachable ();
16983 }
16984
16985 /* Now, follow the control flow (except subroutine calls). */
16986 switch (l->dw_loc_opc)
16987 {
16988 case DW_OP_bra:
16989 if (!resolve_args_picking_1 (l->dw_loc_next, frame_offset_, dpi,
16990 frame_offsets))
16991 return false;
16992 /* Fall through. */
16993
16994 case DW_OP_skip:
16995 l = l->dw_loc_oprnd1.v.val_loc;
16996 break;
16997
16998 case DW_OP_stack_value:
16999 return true;
17000
17001 default:
17002 l = l->dw_loc_next;
17003 break;
17004 }
17005 }
17006
17007 return true;
17008}
17009
17010/* Make a DFS over operations reachable through LOC (i.e. follow branch
17011 operations) in order to resolve the operand of DW_OP_pick operations that
17012 target DWARF procedure arguments (DPI). INITIAL_FRAME_OFFSET is the frame
17013 offset *before* LOC is executed. Return if all relocations were
17014 successful. */
17015
17016static bool
17017resolve_args_picking (dw_loc_descr_ref loc, unsigned initial_frame_offset,
17018 struct dwarf_procedure_info *dpi)
17019{
17020 /* Associate to all visited operations the frame offset *before* evaluating
17021 this operation. */
17022 hash_map<dw_loc_descr_ref, unsigned> frame_offsets;
17023
17024 return resolve_args_picking_1 (loc, initial_frame_offset, dpi,
17025 frame_offsets);
17026}
17027
17028/* Try to generate a DWARF procedure that computes the same result as FNDECL.
17029 Return NULL if it is not possible. */
17030
17031static dw_die_ref
17032function_to_dwarf_procedure (tree fndecl)
17033{
17034 struct loc_descr_context ctx;
17035 struct dwarf_procedure_info dpi;
17036 dw_die_ref dwarf_proc_die;
17037 tree tree_body = DECL_SAVED_TREE (fndecl);
17038 dw_loc_descr_ref loc_body, epilogue;
17039
17040 tree cursor;
17041 unsigned i;
17042
17043 /* Do not generate multiple DWARF procedures for the same function
17044 declaration. */
17045 dwarf_proc_die = lookup_decl_die (fndecl);
17046 if (dwarf_proc_die != NULL)
17047 return dwarf_proc_die;
17048
17049 /* DWARF procedures are available starting with the DWARFv3 standard. */
17050 if (dwarf_version < 3 && dwarf_strict)
17051 return NULL;
17052
17053 /* We handle only functions for which we still have a body, that return a
17054 supported type and that takes arguments with supported types. Note that
17055 there is no point translating functions that return nothing. */
17056 if (tree_body == NULL_TREE
17057 || DECL_RESULT (fndecl) == NULL_TREE
17058 || !is_handled_procedure_type (TREE_TYPE (DECL_RESULT (fndecl))))
17059 return NULL;
17060
17061 for (cursor = DECL_ARGUMENTS (fndecl);
17062 cursor != NULL_TREE;
17063 cursor = TREE_CHAIN (cursor))
17064 if (!is_handled_procedure_type (TREE_TYPE (cursor)))
17065 return NULL;
17066
17067 /* Match only "expr" in: RETURN_EXPR (MODIFY_EXPR (RESULT_DECL, expr)). */
17068 if (TREE_CODE (tree_body) != RETURN_EXPR)
17069 return NULL;
17070 tree_body = TREE_OPERAND (tree_body, 0);
17071 if (TREE_CODE (tree_body) != MODIFY_EXPR
17072 || TREE_OPERAND (tree_body, 0) != DECL_RESULT (fndecl))
17073 return NULL;
17074 tree_body = TREE_OPERAND (tree_body, 1);
17075
17076 /* Try to translate the body expression itself. Note that this will probably
17077 cause an infinite recursion if its call graph has a cycle. This is very
17078 unlikely for size functions, however, so don't bother with such things at
17079 the moment. */
17080 ctx.context_type = NULL_TREE;
17081 ctx.base_decl = NULL_TREE;
17082 ctx.dpi = &dpi;
17083 ctx.placeholder_arg = false;
17084 ctx.placeholder_seen = false;
17085 dpi.fndecl = fndecl;
17086 dpi.args_count = list_length (DECL_ARGUMENTS (fndecl));
17087 loc_body = loc_descriptor_from_tree (tree_body, 0, &ctx);
17088 if (!loc_body)
17089 return NULL;
17090
17091 /* After evaluating all operands in "loc_body", we should still have on the
17092 stack all arguments plus the desired function result (top of the stack).
17093 Generate code in order to keep only the result in our stack frame. */
17094 epilogue = NULL;
17095 for (i = 0; i < dpi.args_count; ++i)
17096 {
17097 dw_loc_descr_ref op_couple = new_loc_descr (DW_OP_swap, 0, 0);
17098 op_couple->dw_loc_next = new_loc_descr (DW_OP_drop, 0, 0);
17099 op_couple->dw_loc_next->dw_loc_next = epilogue;
17100 epilogue = op_couple;
17101 }
17102 add_loc_descr (&loc_body, epilogue);
17103 if (!resolve_args_picking (loc_body, dpi.args_count, &dpi))
17104 return NULL;
17105
17106 /* Trailing nops from loc_descriptor_from_tree (if any) cannot be removed
17107 because they are considered useful. Now there is an epilogue, they are
17108 not anymore, so give it another try. */
17109 loc_descr_without_nops (loc_body);
17110
17111 /* fndecl may be used both as a regular DW_TAG_subprogram DIE and as
17112 a DW_TAG_dwarf_procedure, so we may have a conflict, here. It's unlikely,
17113 though, given that size functions do not come from source, so they should
17114 not have a dedicated DW_TAG_subprogram DIE. */
17115 dwarf_proc_die
17116 = new_dwarf_proc_die (loc_body, fndecl,
17117 get_context_die (DECL_CONTEXT (fndecl)));
17118
17119 /* The called DWARF procedure consumes one stack slot per argument and
17120 returns one stack slot. */
17121 dwarf_proc_stack_usage_map->put (dwarf_proc_die, 1 - dpi.args_count);
17122
17123 return dwarf_proc_die;
17124}
17125
17126
17127/* Generate Dwarf location list representing LOC.
17128 If WANT_ADDRESS is false, expression computing LOC will be computed
17129 If WANT_ADDRESS is 1, expression computing address of LOC will be returned
17130 if WANT_ADDRESS is 2, expression computing address useable in location
17131 will be returned (i.e. DW_OP_reg can be used
17132 to refer to register values).
17133
17134 CONTEXT provides information to customize the location descriptions
17135 generation. Its context_type field specifies what type is implicitly
17136 referenced by DW_OP_push_object_address. If it is NULL_TREE, this operation
17137 will not be generated.
17138
17139 Its DPI field determines whether we are generating a DWARF expression for a
17140 DWARF procedure, so PARM_DECL references are processed specifically.
17141
17142 If CONTEXT is NULL, the behavior is the same as if context_type, base_decl
17143 and dpi fields were null. */
17144
17145static dw_loc_list_ref
17146loc_list_from_tree_1 (tree loc, int want_address,
17147 struct loc_descr_context *context)
17148{
17149 dw_loc_descr_ref ret = NULL, ret1 = NULL;
17150 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
17151 int have_address = 0;
17152 enum dwarf_location_atom op;
17153
17154 /* ??? Most of the time we do not take proper care for sign/zero
17155 extending the values properly. Hopefully this won't be a real
17156 problem... */
17157
17158 if (context != NULL
17159 && context->base_decl == loc
17160 && want_address == 0)
17161 {
17162 if (dwarf_version >= 3 || !dwarf_strict)
17163 return new_loc_list (new_loc_descr (DW_OP_push_object_address, 0, 0),
17164 NULL, NULL, NULL);
17165 else
17166 return NULL;
17167 }
17168
17169 switch (TREE_CODE (loc))
17170 {
17171 case ERROR_MARK:
17172 expansion_failed (loc, NULL_RTX, "ERROR_MARK");
17173 return 0;
17174
17175 case PLACEHOLDER_EXPR:
17176 /* This case involves extracting fields from an object to determine the
17177 position of other fields. It is supposed to appear only as the first
17178 operand of COMPONENT_REF nodes and to reference precisely the type
17179 that the context allows. */
17180 if (context != NULL
17181 && TREE_TYPE (loc) == context->context_type
17182 && want_address >= 1)
17183 {
17184 if (dwarf_version >= 3 || !dwarf_strict)
17185 {
17186 ret = new_loc_descr (DW_OP_push_object_address, 0, 0);
17187 have_address = 1;
17188 break;
17189 }
17190 else
17191 return NULL;
17192 }
17193 /* For DW_TAG_generic_subrange attributes, PLACEHOLDER_EXPR stands for
17194 the single argument passed by consumer. */
17195 else if (context != NULL
17196 && context->placeholder_arg
17197 && INTEGRAL_TYPE_P (TREE_TYPE (loc))
17198 && want_address == 0)
17199 {
17200 ret = new_loc_descr (DW_OP_pick, 0, 0);
17201 ret->frame_offset_rel = 1;
17202 context->placeholder_seen = true;
17203 break;
17204 }
17205 else
17206 expansion_failed (loc, NULL_RTX,
17207 "PLACEHOLDER_EXPR for an unexpected type");
17208 break;
17209
17210 case CALL_EXPR:
17211 {
17212 const int nargs = call_expr_nargs (loc);
17213 tree callee = get_callee_fndecl (loc);
17214 int i;
17215 dw_die_ref dwarf_proc;
17216
17217 if (callee == NULL_TREE)
17218 goto call_expansion_failed;
17219
17220 /* We handle only functions that return an integer. */
17221 if (!is_handled_procedure_type (TREE_TYPE (TREE_TYPE (callee))))
17222 goto call_expansion_failed;
17223
17224 dwarf_proc = function_to_dwarf_procedure (callee);
17225 if (dwarf_proc == NULL)
17226 goto call_expansion_failed;
17227
17228 /* Evaluate arguments right-to-left so that the first argument will
17229 be the top-most one on the stack. */
17230 for (i = nargs - 1; i >= 0; --i)
17231 {
17232 dw_loc_descr_ref loc_descr
17233 = loc_descriptor_from_tree (CALL_EXPR_ARG (loc, i), 0,
17234 context);
17235
17236 if (loc_descr == NULL)
17237 goto call_expansion_failed;
17238
17239 add_loc_descr (&ret, loc_descr);
17240 }
17241
17242 ret1 = new_loc_descr (DW_OP_call4, 0, 0);
17243 ret1->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
17244 ret1->dw_loc_oprnd1.v.val_die_ref.die = dwarf_proc;
17245 ret1->dw_loc_oprnd1.v.val_die_ref.external = 0;
17246 add_loc_descr (&ret, ret1);
17247 break;
17248
17249 call_expansion_failed:
17250 expansion_failed (loc, NULL_RTX, "CALL_EXPR");
17251 /* There are no opcodes for these operations. */
17252 return 0;
17253 }
17254
17255 case PREINCREMENT_EXPR:
17256 case PREDECREMENT_EXPR:
17257 case POSTINCREMENT_EXPR:
17258 case POSTDECREMENT_EXPR:
17259 expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
17260 /* There are no opcodes for these operations. */
17261 return 0;
17262
17263 case ADDR_EXPR:
17264 /* If we already want an address, see if there is INDIRECT_REF inside
17265 e.g. for &this->field. */
17266 if (want_address)
17267 {
17268 list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
17269 (loc, want_address == 2, context);
17270 if (list_ret)
17271 have_address = 1;
17272 else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
17273 && (ret = cst_pool_loc_descr (loc)))
17274 have_address = 1;
17275 }
17276 /* Otherwise, process the argument and look for the address. */
17277 if (!list_ret && !ret)
17278 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 1, context);
17279 else
17280 {
17281 if (want_address)
17282 expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
17283 return NULL;
17284 }
17285 break;
17286
17287 case VAR_DECL:
17288 if (DECL_THREAD_LOCAL_P (loc))
17289 {
17290 rtx rtl;
17291 enum dwarf_location_atom tls_op;
17292 enum dtprel_bool dtprel = dtprel_false;
17293
17294 if (targetm.have_tls)
17295 {
17296 /* If this is not defined, we have no way to emit the
17297 data. */
17298 if (!targetm.asm_out.output_dwarf_dtprel)
17299 return 0;
17300
17301 /* The way DW_OP_GNU_push_tls_address is specified, we
17302 can only look up addresses of objects in the current
17303 module. We used DW_OP_addr as first op, but that's
17304 wrong, because DW_OP_addr is relocated by the debug
17305 info consumer, while DW_OP_GNU_push_tls_address
17306 operand shouldn't be. */
17307 if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
17308 return 0;
17309 dtprel = dtprel_true;
17310 /* We check for DWARF 5 here because gdb did not implement
17311 DW_OP_form_tls_address until after 7.12. */
17312 tls_op = (dwarf_version >= 5 ? DW_OP_form_tls_address
17313 : DW_OP_GNU_push_tls_address);
17314 }
17315 else
17316 {
17317 if (!targetm.emutls.debug_form_tls_address
17318 || !(dwarf_version >= 3 || !dwarf_strict))
17319 return 0;
17320 /* We stuffed the control variable into the DECL_VALUE_EXPR
17321 to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
17322 no longer appear in gimple code. We used the control
17323 variable in specific so that we could pick it up here. */
17324 loc = DECL_VALUE_EXPR (loc);
17325 tls_op = DW_OP_form_tls_address;
17326 }
17327
17328 rtl = rtl_for_decl_location (loc);
17329 if (rtl == NULL_RTX)
17330 return 0;
17331
17332 if (!MEM_P (rtl))
17333 return 0;
17334 rtl = XEXP (rtl, 0);
17335 if (! CONSTANT_P (rtl))
17336 return 0;
17337
17338 ret = new_addr_loc_descr (rtl, dtprel);
17339 ret1 = new_loc_descr (tls_op, 0, 0);
17340 add_loc_descr (&ret, ret1);
17341
17342 have_address = 1;
17343 break;
17344 }
17345 /* FALLTHRU */
17346
17347 case PARM_DECL:
17348 if (context != NULL && context->dpi != NULL
17349 && DECL_CONTEXT (loc) == context->dpi->fndecl)
17350 {
17351 /* We are generating code for a DWARF procedure and we want to access
17352 one of its arguments: find the appropriate argument offset and let
17353 the resolve_args_picking pass compute the offset that complies
17354 with the stack frame size. */
17355 unsigned i = 0;
17356 tree cursor;
17357
17358 for (cursor = DECL_ARGUMENTS (context->dpi->fndecl);
17359 cursor != NULL_TREE && cursor != loc;
17360 cursor = TREE_CHAIN (cursor), ++i)
17361 ;
17362 /* If we are translating a DWARF procedure, all referenced parameters
17363 must belong to the current function. */
17364 gcc_assert (cursor != NULL_TREE);
17365
17366 ret = new_loc_descr (DW_OP_pick, i, 0);
17367 ret->frame_offset_rel = 1;
17368 break;
17369 }
17370 /* FALLTHRU */
17371
17372 case RESULT_DECL:
17373 if (DECL_HAS_VALUE_EXPR_P (loc))
17374 return loc_list_from_tree_1 (DECL_VALUE_EXPR (loc),
17375 want_address, context);
17376 /* FALLTHRU */
17377
17378 case FUNCTION_DECL:
17379 {
17380 rtx rtl;
17381 var_loc_list *loc_list = lookup_decl_loc (loc);
17382
17383 if (loc_list && loc_list->first)
17384 {
17385 list_ret = dw_loc_list (loc_list, loc, want_address);
17386 have_address = want_address != 0;
17387 break;
17388 }
17389 rtl = rtl_for_decl_location (loc);
17390 if (rtl == NULL_RTX)
17391 {
17392 if (TREE_CODE (loc) != FUNCTION_DECL
17393 && early_dwarf
17394 && current_function_decl
17395 && want_address != 1
17396 && (INTEGRAL_TYPE_P (TREE_TYPE (loc))
17397 || POINTER_TYPE_P (TREE_TYPE (loc)))
17398 && DECL_CONTEXT (loc) == current_function_decl
17399 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (loc)))
17400 <= DWARF2_ADDR_SIZE))
17401 {
17402 dw_die_ref ref = lookup_decl_die (loc);
17403 ret = new_loc_descr (DW_OP_GNU_variable_value, 0, 0);
17404 if (ref)
17405 {
17406 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
17407 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
17408 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
17409 }
17410 else
17411 {
17412 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
17413 ret->dw_loc_oprnd1.v.val_decl_ref = loc;
17414 }
17415 break;
17416 }
17417 expansion_failed (loc, NULL_RTX, "DECL has no RTL");
17418 return 0;
17419 }
17420 else if (CONST_INT_P (rtl))
17421 {
17422 HOST_WIDE_INT val = INTVAL (rtl);
17423 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
17424 val &= GET_MODE_MASK (DECL_MODE (loc));
17425 ret = int_loc_descriptor (val);
17426 }
17427 else if (GET_CODE (rtl) == CONST_STRING)
17428 {
17429 expansion_failed (loc, NULL_RTX, "CONST_STRING");
17430 return 0;
17431 }
17432 else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
17433 ret = new_addr_loc_descr (rtl, dtprel_false);
17434 else
17435 {
17436 machine_mode mode, mem_mode;
17437
17438 /* Certain constructs can only be represented at top-level. */
17439 if (want_address == 2)
17440 {
17441 ret = loc_descriptor (rtl, VOIDmode,
17442 VAR_INIT_STATUS_INITIALIZED);
17443 have_address = 1;
17444 }
17445 else
17446 {
17447 mode = GET_MODE (rtl);
17448 mem_mode = VOIDmode;
17449 if (MEM_P (rtl))
17450 {
17451 mem_mode = mode;
17452 mode = get_address_mode (rtl);
17453 rtl = XEXP (rtl, 0);
17454 have_address = 1;
17455 }
17456 ret = mem_loc_descriptor (rtl, mode, mem_mode,
17457 VAR_INIT_STATUS_INITIALIZED);
17458 }
17459 if (!ret)
17460 expansion_failed (loc, rtl,
17461 "failed to produce loc descriptor for rtl");
17462 }
17463 }
17464 break;
17465
17466 case MEM_REF:
17467 if (!integer_zerop (TREE_OPERAND (loc, 1)))
17468 {
17469 have_address = 1;
17470 goto do_plus;
17471 }
17472 /* Fallthru. */
17473 case INDIRECT_REF:
17474 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17475 have_address = 1;
17476 break;
17477
17478 case TARGET_MEM_REF:
17479 case SSA_NAME:
17480 case DEBUG_EXPR_DECL:
17481 return NULL;
17482
17483 case COMPOUND_EXPR:
17484 return loc_list_from_tree_1 (TREE_OPERAND (loc, 1), want_address,
17485 context);
17486
17487 CASE_CONVERT:
17488 case VIEW_CONVERT_EXPR:
17489 case SAVE_EXPR:
17490 case MODIFY_EXPR:
17491 case NON_LVALUE_EXPR:
17492 return loc_list_from_tree_1 (TREE_OPERAND (loc, 0), want_address,
17493 context);
17494
17495 case COMPONENT_REF:
17496 case BIT_FIELD_REF:
17497 case ARRAY_REF:
17498 case ARRAY_RANGE_REF:
17499 case REALPART_EXPR:
17500 case IMAGPART_EXPR:
17501 {
17502 tree obj, offset;
17503 HOST_WIDE_INT bitsize, bitpos, bytepos;
17504 machine_mode mode;
17505 int unsignedp, reversep, volatilep = 0;
17506
17507 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
17508 &unsignedp, &reversep, &volatilep);
17509
17510 gcc_assert (obj != loc);
17511
17512 list_ret = loc_list_from_tree_1 (obj,
17513 want_address == 2
17514 && !bitpos && !offset ? 2 : 1,
17515 context);
17516 /* TODO: We can extract value of the small expression via shifting even
17517 for nonzero bitpos. */
17518 if (list_ret == 0)
17519 return 0;
17520 if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
17521 {
17522 expansion_failed (loc, NULL_RTX,
17523 "bitfield access");
17524 return 0;
17525 }
17526
17527 if (offset != NULL_TREE)
17528 {
17529 /* Variable offset. */
17530 list_ret1 = loc_list_from_tree_1 (offset, 0, context);
17531 if (list_ret1 == 0)
17532 return 0;
17533 add_loc_list (&list_ret, list_ret1);
17534 if (!list_ret)
17535 return 0;
17536 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
17537 }
17538
17539 bytepos = bitpos / BITS_PER_UNIT;
17540 if (bytepos > 0)
17541 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
17542 else if (bytepos < 0)
17543 loc_list_plus_const (list_ret, bytepos);
17544
17545 have_address = 1;
17546 break;
17547 }
17548
17549 case INTEGER_CST:
17550 if ((want_address || !tree_fits_shwi_p (loc))
17551 && (ret = cst_pool_loc_descr (loc)))
17552 have_address = 1;
17553 else if (want_address == 2
17554 && tree_fits_shwi_p (loc)
17555 && (ret = address_of_int_loc_descriptor
17556 (int_size_in_bytes (TREE_TYPE (loc)),
17557 tree_to_shwi (loc))))
17558 have_address = 1;
17559 else if (tree_fits_shwi_p (loc))
17560 ret = int_loc_descriptor (tree_to_shwi (loc));
17561 else if (tree_fits_uhwi_p (loc))
17562 ret = uint_loc_descriptor (tree_to_uhwi (loc));
17563 else
17564 {
17565 expansion_failed (loc, NULL_RTX,
17566 "Integer operand is not host integer");
17567 return 0;
17568 }
17569 break;
17570
17571 case CONSTRUCTOR:
17572 case REAL_CST:
17573 case STRING_CST:
17574 case COMPLEX_CST:
17575 if ((ret = cst_pool_loc_descr (loc)))
17576 have_address = 1;
17577 else if (TREE_CODE (loc) == CONSTRUCTOR)
17578 {
17579 tree type = TREE_TYPE (loc);
17580 unsigned HOST_WIDE_INT size = int_size_in_bytes (type);
17581 unsigned HOST_WIDE_INT offset = 0;
17582 unsigned HOST_WIDE_INT cnt;
17583 constructor_elt *ce;
17584
17585 if (TREE_CODE (type) == RECORD_TYPE)
17586 {
17587 /* This is very limited, but it's enough to output
17588 pointers to member functions, as long as the
17589 referenced function is defined in the current
17590 translation unit. */
17591 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (loc), cnt, ce)
17592 {
17593 tree val = ce->value;
17594
17595 tree field = ce->index;
17596
17597 if (val)
17598 STRIP_NOPS (val);
17599
17600 if (!field || DECL_BIT_FIELD (field))
17601 {
17602 expansion_failed (loc, NULL_RTX,
17603 "bitfield in record type constructor");
17604 size = offset = (unsigned HOST_WIDE_INT)-1;
17605 ret = NULL;
17606 break;
17607 }
17608
17609 HOST_WIDE_INT fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
17610 unsigned HOST_WIDE_INT pos = int_byte_position (field);
17611 gcc_assert (pos + fieldsize <= size);
17612 if (pos < offset)
17613 {
17614 expansion_failed (loc, NULL_RTX,
17615 "out-of-order fields in record constructor");
17616 size = offset = (unsigned HOST_WIDE_INT)-1;
17617 ret = NULL;
17618 break;
17619 }
17620 if (pos > offset)
17621 {
17622 ret1 = new_loc_descr (DW_OP_piece, pos - offset, 0);
17623 add_loc_descr (&ret, ret1);
17624 offset = pos;
17625 }
17626 if (val && fieldsize != 0)
17627 {
17628 ret1 = loc_descriptor_from_tree (val, want_address, context);
17629 if (!ret1)
17630 {
17631 expansion_failed (loc, NULL_RTX,
17632 "unsupported expression in field");
17633 size = offset = (unsigned HOST_WIDE_INT)-1;
17634 ret = NULL;
17635 break;
17636 }
17637 add_loc_descr (&ret, ret1);
17638 }
17639 if (fieldsize)
17640 {
17641 ret1 = new_loc_descr (DW_OP_piece, fieldsize, 0);
17642 add_loc_descr (&ret, ret1);
17643 offset = pos + fieldsize;
17644 }
17645 }
17646
17647 if (offset != size)
17648 {
17649 ret1 = new_loc_descr (DW_OP_piece, size - offset, 0);
17650 add_loc_descr (&ret, ret1);
17651 offset = size;
17652 }
17653
17654 have_address = !!want_address;
17655 }
17656 else
17657 expansion_failed (loc, NULL_RTX,
17658 "constructor of non-record type");
17659 }
17660 else
17661 /* We can construct small constants here using int_loc_descriptor. */
17662 expansion_failed (loc, NULL_RTX,
17663 "constructor or constant not in constant pool");
17664 break;
17665
17666 case TRUTH_AND_EXPR:
17667 case TRUTH_ANDIF_EXPR:
17668 case BIT_AND_EXPR:
17669 op = DW_OP_and;
17670 goto do_binop;
17671
17672 case TRUTH_XOR_EXPR:
17673 case BIT_XOR_EXPR:
17674 op = DW_OP_xor;
17675 goto do_binop;
17676
17677 case TRUTH_OR_EXPR:
17678 case TRUTH_ORIF_EXPR:
17679 case BIT_IOR_EXPR:
17680 op = DW_OP_or;
17681 goto do_binop;
17682
17683 case FLOOR_DIV_EXPR:
17684 case CEIL_DIV_EXPR:
17685 case ROUND_DIV_EXPR:
17686 case TRUNC_DIV_EXPR:
17687 case EXACT_DIV_EXPR:
17688 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
17689 return 0;
17690 op = DW_OP_div;
17691 goto do_binop;
17692
17693 case MINUS_EXPR:
17694 op = DW_OP_minus;
17695 goto do_binop;
17696
17697 case FLOOR_MOD_EXPR:
17698 case CEIL_MOD_EXPR:
17699 case ROUND_MOD_EXPR:
17700 case TRUNC_MOD_EXPR:
17701 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
17702 {
17703 op = DW_OP_mod;
17704 goto do_binop;
17705 }
17706 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17707 list_ret1 = loc_list_from_tree_1 (TREE_OPERAND (loc, 1), 0, context);
17708 if (list_ret == 0 || list_ret1 == 0)
17709 return 0;
17710
17711 add_loc_list (&list_ret, list_ret1);
17712 if (list_ret == 0)
17713 return 0;
17714 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
17715 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
17716 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
17717 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
17718 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
17719 break;
17720
17721 case MULT_EXPR:
17722 op = DW_OP_mul;
17723 goto do_binop;
17724
17725 case LSHIFT_EXPR:
17726 op = DW_OP_shl;
17727 goto do_binop;
17728
17729 case RSHIFT_EXPR:
17730 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
17731 goto do_binop;
17732
17733 case POINTER_PLUS_EXPR:
17734 case PLUS_EXPR:
17735 do_plus:
17736 if (tree_fits_shwi_p (TREE_OPERAND (loc, 1)))
17737 {
17738 /* Big unsigned numbers can fit in HOST_WIDE_INT but it may be
17739 smarter to encode their opposite. The DW_OP_plus_uconst operation
17740 takes 1 + X bytes, X being the size of the ULEB128 addend. On the
17741 other hand, a "<push literal>; DW_OP_minus" pattern takes 1 + Y
17742 bytes, Y being the size of the operation that pushes the opposite
17743 of the addend. So let's choose the smallest representation. */
17744 const tree tree_addend = TREE_OPERAND (loc, 1);
17745 offset_int wi_addend;
17746 HOST_WIDE_INT shwi_addend;
17747 dw_loc_descr_ref loc_naddend;
17748
17749 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17750 if (list_ret == 0)
17751 return 0;
17752
17753 /* Try to get the literal to push. It is the opposite of the addend,
17754 so as we rely on wrapping during DWARF evaluation, first decode
17755 the literal as a "DWARF-sized" signed number. */
17756 wi_addend = wi::to_offset (tree_addend);
17757 wi_addend = wi::sext (wi_addend, DWARF2_ADDR_SIZE * 8);
17758 shwi_addend = wi_addend.to_shwi ();
17759 loc_naddend = (shwi_addend != INTTYPE_MINIMUM (HOST_WIDE_INT))
17760 ? int_loc_descriptor (-shwi_addend)
17761 : NULL;
17762
17763 if (loc_naddend != NULL
17764 && ((unsigned) size_of_uleb128 (shwi_addend)
17765 > size_of_loc_descr (loc_naddend)))
17766 {
17767 add_loc_descr_to_each (list_ret, loc_naddend);
17768 add_loc_descr_to_each (list_ret,
17769 new_loc_descr (DW_OP_minus, 0, 0));
17770 }
17771 else
17772 {
17773 for (dw_loc_descr_ref loc_cur = loc_naddend; loc_cur != NULL; )
17774 {
17775 loc_naddend = loc_cur;
17776 loc_cur = loc_cur->dw_loc_next;
17777 ggc_free (loc_naddend);
17778 }
17779 loc_list_plus_const (list_ret, wi_addend.to_shwi ());
17780 }
17781 break;
17782 }
17783
17784 op = DW_OP_plus;
17785 goto do_binop;
17786
17787 case LE_EXPR:
17788 op = DW_OP_le;
17789 goto do_comp_binop;
17790
17791 case GE_EXPR:
17792 op = DW_OP_ge;
17793 goto do_comp_binop;
17794
17795 case LT_EXPR:
17796 op = DW_OP_lt;
17797 goto do_comp_binop;
17798
17799 case GT_EXPR:
17800 op = DW_OP_gt;
17801 goto do_comp_binop;
17802
17803 do_comp_binop:
17804 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
17805 {
17806 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
17807 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0, context);
17808 list_ret = loc_list_from_uint_comparison (list_ret, list_ret1,
17809 TREE_CODE (loc));
17810 break;
17811 }
17812 else
17813 goto do_binop;
17814
17815 case EQ_EXPR:
17816 op = DW_OP_eq;
17817 goto do_binop;
17818
17819 case NE_EXPR:
17820 op = DW_OP_ne;
17821 goto do_binop;
17822
17823 do_binop:
17824 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17825 list_ret1 = loc_list_from_tree_1 (TREE_OPERAND (loc, 1), 0, context);
17826 if (list_ret == 0 || list_ret1 == 0)
17827 return 0;
17828
17829 add_loc_list (&list_ret, list_ret1);
17830 if (list_ret == 0)
17831 return 0;
17832 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
17833 break;
17834
17835 case TRUTH_NOT_EXPR:
17836 case BIT_NOT_EXPR:
17837 op = DW_OP_not;
17838 goto do_unop;
17839
17840 case ABS_EXPR:
17841 op = DW_OP_abs;
17842 goto do_unop;
17843
17844 case NEGATE_EXPR:
17845 op = DW_OP_neg;
17846 goto do_unop;
17847
17848 do_unop:
17849 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17850 if (list_ret == 0)
17851 return 0;
17852
17853 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
17854 break;
17855
17856 case MIN_EXPR:
17857 case MAX_EXPR:
17858 {
17859 const enum tree_code code =
17860 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
17861
17862 loc = build3 (COND_EXPR, TREE_TYPE (loc),
17863 build2 (code, integer_type_node,
17864 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
17865 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
17866 }
17867
17868 /* fall through */
17869
17870 case COND_EXPR:
17871 {
17872 dw_loc_descr_ref lhs
17873 = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0, context);
17874 dw_loc_list_ref rhs
17875 = loc_list_from_tree_1 (TREE_OPERAND (loc, 2), 0, context);
17876 dw_loc_descr_ref bra_node, jump_node, tmp;
17877
17878 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17879 if (list_ret == 0 || lhs == 0 || rhs == 0)
17880 return 0;
17881
17882 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
17883 add_loc_descr_to_each (list_ret, bra_node);
17884
17885 add_loc_list (&list_ret, rhs);
17886 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
17887 add_loc_descr_to_each (list_ret, jump_node);
17888
17889 add_loc_descr_to_each (list_ret, lhs);
17890 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
17891 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
17892
17893 /* ??? Need a node to point the skip at. Use a nop. */
17894 tmp = new_loc_descr (DW_OP_nop, 0, 0);
17895 add_loc_descr_to_each (list_ret, tmp);
17896 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
17897 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
17898 }
17899 break;
17900
17901 case FIX_TRUNC_EXPR:
17902 return 0;
17903
17904 default:
17905 /* Leave front-end specific codes as simply unknown. This comes
17906 up, for instance, with the C STMT_EXPR. */
17907 if ((unsigned int) TREE_CODE (loc)
17908 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
17909 {
17910 expansion_failed (loc, NULL_RTX,
17911 "language specific tree node");
17912 return 0;
17913 }
17914
17915 /* Otherwise this is a generic code; we should just lists all of
17916 these explicitly. We forgot one. */
17917 if (flag_checking)
17918 gcc_unreachable ();
17919
17920 /* In a release build, we want to degrade gracefully: better to
17921 generate incomplete debugging information than to crash. */
17922 return NULL;
17923 }
17924
17925 if (!ret && !list_ret)
17926 return 0;
17927
17928 if (want_address == 2 && !have_address
17929 && (dwarf_version >= 4 || !dwarf_strict))
17930 {
17931 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
17932 {
17933 expansion_failed (loc, NULL_RTX,
17934 "DWARF address size mismatch");
17935 return 0;
17936 }
17937 if (ret)
17938 add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
17939 else
17940 add_loc_descr_to_each (list_ret,
17941 new_loc_descr (DW_OP_stack_value, 0, 0));
17942 have_address = 1;
17943 }
17944 /* Show if we can't fill the request for an address. */
17945 if (want_address && !have_address)
17946 {
17947 expansion_failed (loc, NULL_RTX,
17948 "Want address and only have value");
17949 return 0;
17950 }
17951
17952 gcc_assert (!ret || !list_ret);
17953
17954 /* If we've got an address and don't want one, dereference. */
17955 if (!want_address && have_address)
17956 {
17957 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
17958
17959 if (size > DWARF2_ADDR_SIZE || size == -1)
17960 {
17961 expansion_failed (loc, NULL_RTX,
17962 "DWARF address size mismatch");
17963 return 0;
17964 }
17965 else if (size == DWARF2_ADDR_SIZE)
17966 op = DW_OP_deref;
17967 else
17968 op = DW_OP_deref_size;
17969
17970 if (ret)
17971 add_loc_descr (&ret, new_loc_descr (op, size, 0));
17972 else
17973 add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
17974 }
17975 if (ret)
17976 list_ret = new_loc_list (ret, NULL, NULL, NULL);
17977
17978 return list_ret;
17979}
17980
17981/* Likewise, but strip useless DW_OP_nop operations in the resulting
17982 expressions. */
17983
17984static dw_loc_list_ref
17985loc_list_from_tree (tree loc, int want_address,
17986 struct loc_descr_context *context)
17987{
17988 dw_loc_list_ref result = loc_list_from_tree_1 (loc, want_address, context);
17989
17990 for (dw_loc_list_ref loc_cur = result;
17991 loc_cur != NULL; loc_cur = loc_cur->dw_loc_next)
17992 loc_descr_without_nops (loc_cur->expr);
17993 return result;
17994}
17995
17996/* Same as above but return only single location expression. */
17997static dw_loc_descr_ref
17998loc_descriptor_from_tree (tree loc, int want_address,
17999 struct loc_descr_context *context)
18000{
18001 dw_loc_list_ref ret = loc_list_from_tree (loc, want_address, context);
18002 if (!ret)
18003 return NULL;
18004 if (ret->dw_loc_next)
18005 {
18006 expansion_failed (loc, NULL_RTX,
18007 "Location list where only loc descriptor needed");
18008 return NULL;
18009 }
18010 return ret->expr;
18011}
18012
18013/* Given a value, round it up to the lowest multiple of `boundary'
18014 which is not less than the value itself. */
18015
18016static inline HOST_WIDE_INT
18017ceiling (HOST_WIDE_INT value, unsigned int boundary)
18018{
18019 return (((value + boundary - 1) / boundary) * boundary);
18020}
18021
18022/* Given a pointer to what is assumed to be a FIELD_DECL node, return a
18023 pointer to the declared type for the relevant field variable, or return
18024 `integer_type_node' if the given node turns out to be an
18025 ERROR_MARK node. */
18026
18027static inline tree
18028field_type (const_tree decl)
18029{
18030 tree type;
18031
18032 if (TREE_CODE (decl) == ERROR_MARK)
18033 return integer_type_node;
18034
18035 type = DECL_BIT_FIELD_TYPE (decl);
18036 if (type == NULL_TREE)
18037 type = TREE_TYPE (decl);
18038
18039 return type;
18040}
18041
18042/* Given a pointer to a tree node, return the alignment in bits for
18043 it, or else return BITS_PER_WORD if the node actually turns out to
18044 be an ERROR_MARK node. */
18045
18046static inline unsigned
18047simple_type_align_in_bits (const_tree type)
18048{
18049 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
18050}
18051
18052static inline unsigned
18053simple_decl_align_in_bits (const_tree decl)
18054{
18055 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
18056}
18057
18058/* Return the result of rounding T up to ALIGN. */
18059
18060static inline offset_int
18061round_up_to_align (const offset_int &t, unsigned int align)
18062{
18063 return wi::udiv_trunc (t + align - 1, align) * align;
18064}
18065
18066/* Compute the size of TYPE in bytes. If possible, return NULL and store the
18067 size as an integer constant in CST_SIZE. Otherwise, if possible, return a
18068 DWARF expression that computes the size. Return NULL and set CST_SIZE to -1
18069 if we fail to return the size in one of these two forms. */
18070
18071static dw_loc_descr_ref
18072type_byte_size (const_tree type, HOST_WIDE_INT *cst_size)
18073{
18074 tree tree_size;
18075 struct loc_descr_context ctx;
18076
18077 /* Return a constant integer in priority, if possible. */
18078 *cst_size = int_size_in_bytes (type);
18079 if (*cst_size != -1)
18080 return NULL;
18081
18082 ctx.context_type = const_cast<tree> (type);
18083 ctx.base_decl = NULL_TREE;
18084 ctx.dpi = NULL;
18085 ctx.placeholder_arg = false;
18086 ctx.placeholder_seen = false;
18087
18088 type = TYPE_MAIN_VARIANT (type);
18089 tree_size = TYPE_SIZE_UNIT (type);
18090 return ((tree_size != NULL_TREE)
18091 ? loc_descriptor_from_tree (tree_size, 0, &ctx)
18092 : NULL);
18093}
18094
18095/* Helper structure for RECORD_TYPE processing. */
18096struct vlr_context
18097{
18098 /* Root RECORD_TYPE. It is needed to generate data member location
18099 descriptions in variable-length records (VLR), but also to cope with
18100 variants, which are composed of nested structures multiplexed with
18101 QUAL_UNION_TYPE nodes. Each time such a structure is passed to a
18102 function processing a FIELD_DECL, it is required to be non null. */
18103 tree struct_type;
18104 /* When generating a variant part in a RECORD_TYPE (i.e. a nested
18105 QUAL_UNION_TYPE), this holds an expression that computes the offset for
18106 this variant part as part of the root record (in storage units). For
18107 regular records, it must be NULL_TREE. */
18108 tree variant_part_offset;
18109};
18110
18111/* Given a pointer to a FIELD_DECL, compute the byte offset of the lowest
18112 addressed byte of the "containing object" for the given FIELD_DECL. If
18113 possible, return a native constant through CST_OFFSET (in which case NULL is
18114 returned); otherwise return a DWARF expression that computes the offset.
18115
18116 Set *CST_OFFSET to 0 and return NULL if we are unable to determine what
18117 that offset is, either because the argument turns out to be a pointer to an
18118 ERROR_MARK node, or because the offset expression is too complex for us.
18119
18120 CTX is required: see the comment for VLR_CONTEXT. */
18121
18122static dw_loc_descr_ref
18123field_byte_offset (const_tree decl, struct vlr_context *ctx,
18124 HOST_WIDE_INT *cst_offset)
18125{
18126 tree tree_result;
18127 dw_loc_list_ref loc_result;
18128
18129 *cst_offset = 0;
18130
18131 if (TREE_CODE (decl) == ERROR_MARK)
18132 return NULL;
18133 else
18134 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
18135
18136 /* We cannot handle variable bit offsets at the moment, so abort if it's the
18137 case. */
18138 if (TREE_CODE (DECL_FIELD_BIT_OFFSET (decl)) != INTEGER_CST)
18139 return NULL;
18140
18141#ifdef PCC_BITFIELD_TYPE_MATTERS
18142 /* We used to handle only constant offsets in all cases. Now, we handle
18143 properly dynamic byte offsets only when PCC bitfield type doesn't
18144 matter. */
18145 if (PCC_BITFIELD_TYPE_MATTERS
18146 && TREE_CODE (DECL_FIELD_OFFSET (decl)) == INTEGER_CST)
18147 {
18148 offset_int object_offset_in_bits;
18149 offset_int object_offset_in_bytes;
18150 offset_int bitpos_int;
18151 tree type;
18152 tree field_size_tree;
18153 offset_int deepest_bitpos;
18154 offset_int field_size_in_bits;
18155 unsigned int type_align_in_bits;
18156 unsigned int decl_align_in_bits;
18157 offset_int type_size_in_bits;
18158
18159 bitpos_int = wi::to_offset (bit_position (decl));
18160 type = field_type (decl);
18161 type_size_in_bits = offset_int_type_size_in_bits (type);
18162 type_align_in_bits = simple_type_align_in_bits (type);
18163
18164 field_size_tree = DECL_SIZE (decl);
18165
18166 /* The size could be unspecified if there was an error, or for
18167 a flexible array member. */
18168 if (!field_size_tree)
18169 field_size_tree = bitsize_zero_node;
18170
18171 /* If the size of the field is not constant, use the type size. */
18172 if (TREE_CODE (field_size_tree) == INTEGER_CST)
18173 field_size_in_bits = wi::to_offset (field_size_tree);
18174 else
18175 field_size_in_bits = type_size_in_bits;
18176
18177 decl_align_in_bits = simple_decl_align_in_bits (decl);
18178
18179 /* The GCC front-end doesn't make any attempt to keep track of the
18180 starting bit offset (relative to the start of the containing
18181 structure type) of the hypothetical "containing object" for a
18182 bit-field. Thus, when computing the byte offset value for the
18183 start of the "containing object" of a bit-field, we must deduce
18184 this information on our own. This can be rather tricky to do in
18185 some cases. For example, handling the following structure type
18186 definition when compiling for an i386/i486 target (which only
18187 aligns long long's to 32-bit boundaries) can be very tricky:
18188
18189 struct S { int field1; long long field2:31; };
18190
18191 Fortunately, there is a simple rule-of-thumb which can be used
18192 in such cases. When compiling for an i386/i486, GCC will
18193 allocate 8 bytes for the structure shown above. It decides to
18194 do this based upon one simple rule for bit-field allocation.
18195 GCC allocates each "containing object" for each bit-field at
18196 the first (i.e. lowest addressed) legitimate alignment boundary
18197 (based upon the required minimum alignment for the declared
18198 type of the field) which it can possibly use, subject to the
18199 condition that there is still enough available space remaining
18200 in the containing object (when allocated at the selected point)
18201 to fully accommodate all of the bits of the bit-field itself.
18202
18203 This simple rule makes it obvious why GCC allocates 8 bytes for
18204 each object of the structure type shown above. When looking
18205 for a place to allocate the "containing object" for `field2',
18206 the compiler simply tries to allocate a 64-bit "containing
18207 object" at each successive 32-bit boundary (starting at zero)
18208 until it finds a place to allocate that 64- bit field such that
18209 at least 31 contiguous (and previously unallocated) bits remain
18210 within that selected 64 bit field. (As it turns out, for the
18211 example above, the compiler finds it is OK to allocate the
18212 "containing object" 64-bit field at bit-offset zero within the
18213 structure type.)
18214
18215 Here we attempt to work backwards from the limited set of facts
18216 we're given, and we try to deduce from those facts, where GCC
18217 must have believed that the containing object started (within
18218 the structure type). The value we deduce is then used (by the
18219 callers of this routine) to generate DW_AT_location and
18220 DW_AT_bit_offset attributes for fields (both bit-fields and, in
18221 the case of DW_AT_location, regular fields as well). */
18222
18223 /* Figure out the bit-distance from the start of the structure to
18224 the "deepest" bit of the bit-field. */
18225 deepest_bitpos = bitpos_int + field_size_in_bits;
18226
18227 /* This is the tricky part. Use some fancy footwork to deduce
18228 where the lowest addressed bit of the containing object must
18229 be. */
18230 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
18231
18232 /* Round up to type_align by default. This works best for
18233 bitfields. */
18234 object_offset_in_bits
18235 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
18236
18237 if (wi::gtu_p (object_offset_in_bits, bitpos_int))
18238 {
18239 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
18240
18241 /* Round up to decl_align instead. */
18242 object_offset_in_bits
18243 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
18244 }
18245
18246 object_offset_in_bytes
18247 = wi::lrshift (object_offset_in_bits, LOG2_BITS_PER_UNIT);
18248 if (ctx->variant_part_offset == NULL_TREE)
18249 {
18250 *cst_offset = object_offset_in_bytes.to_shwi ();
18251 return NULL;
18252 }
18253 tree_result = wide_int_to_tree (sizetype, object_offset_in_bytes);
18254 }
18255 else
18256#endif /* PCC_BITFIELD_TYPE_MATTERS */
18257 tree_result = byte_position (decl);
18258
18259 if (ctx->variant_part_offset != NULL_TREE)
18260 tree_result = fold_build2 (PLUS_EXPR, TREE_TYPE (tree_result),
18261 ctx->variant_part_offset, tree_result);
18262
18263 /* If the byte offset is a constant, it's simplier to handle a native
18264 constant rather than a DWARF expression. */
18265 if (TREE_CODE (tree_result) == INTEGER_CST)
18266 {
18267 *cst_offset = wi::to_offset (tree_result).to_shwi ();
18268 return NULL;
18269 }
18270 struct loc_descr_context loc_ctx = {
18271 ctx->struct_type, /* context_type */
18272 NULL_TREE, /* base_decl */
18273 NULL, /* dpi */
18274 false, /* placeholder_arg */
18275 false /* placeholder_seen */
18276 };
18277 loc_result = loc_list_from_tree (tree_result, 0, &loc_ctx);
18278
18279 /* We want a DWARF expression: abort if we only have a location list with
18280 multiple elements. */
18281 if (!loc_result || !single_element_loc_list_p (loc_result))
18282 return NULL;
18283 else
18284 return loc_result->expr;
18285}
18286
18287/* The following routines define various Dwarf attributes and any data
18288 associated with them. */
18289
18290/* Add a location description attribute value to a DIE.
18291
18292 This emits location attributes suitable for whole variables and
18293 whole parameters. Note that the location attributes for struct fields are
18294 generated by the routine `data_member_location_attribute' below. */
18295
18296static inline void
18297add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
18298 dw_loc_list_ref descr)
18299{
18300 if (descr == 0)
18301 return;
18302 if (single_element_loc_list_p (descr))
18303 add_AT_loc (die, attr_kind, descr->expr);
18304 else
18305 add_AT_loc_list (die, attr_kind, descr);
18306}
18307
18308/* Add DW_AT_accessibility attribute to DIE if needed. */
18309
18310static void
18311add_accessibility_attribute (dw_die_ref die, tree decl)
18312{
18313 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
18314 children, otherwise the default is DW_ACCESS_public. In DWARF2
18315 the default has always been DW_ACCESS_public. */
18316 if (TREE_PROTECTED (decl))
18317 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
18318 else if (TREE_PRIVATE (decl))
18319 {
18320 if (dwarf_version == 2
18321 || die->die_parent == NULL
18322 || die->die_parent->die_tag != DW_TAG_class_type)
18323 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
18324 }
18325 else if (dwarf_version > 2
18326 && die->die_parent
18327 && die->die_parent->die_tag == DW_TAG_class_type)
18328 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
18329}
18330
18331/* Attach the specialized form of location attribute used for data members of
18332 struct and union types. In the special case of a FIELD_DECL node which
18333 represents a bit-field, the "offset" part of this special location
18334 descriptor must indicate the distance in bytes from the lowest-addressed
18335 byte of the containing struct or union type to the lowest-addressed byte of
18336 the "containing object" for the bit-field. (See the `field_byte_offset'
18337 function above).
18338
18339 For any given bit-field, the "containing object" is a hypothetical object
18340 (of some integral or enum type) within which the given bit-field lives. The
18341 type of this hypothetical "containing object" is always the same as the
18342 declared type of the individual bit-field itself (for GCC anyway... the
18343 DWARF spec doesn't actually mandate this). Note that it is the size (in
18344 bytes) of the hypothetical "containing object" which will be given in the
18345 DW_AT_byte_size attribute for this bit-field. (See the
18346 `byte_size_attribute' function below.) It is also used when calculating the
18347 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
18348 function below.)
18349
18350 CTX is required: see the comment for VLR_CONTEXT. */
18351
18352static void
18353add_data_member_location_attribute (dw_die_ref die,
18354 tree decl,
18355 struct vlr_context *ctx)
18356{
18357 HOST_WIDE_INT offset;
18358 dw_loc_descr_ref loc_descr = 0;
18359
18360 if (TREE_CODE (decl) == TREE_BINFO)
18361 {
18362 /* We're working on the TAG_inheritance for a base class. */
18363 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
18364 {
18365 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
18366 aren't at a fixed offset from all (sub)objects of the same
18367 type. We need to extract the appropriate offset from our
18368 vtable. The following dwarf expression means
18369
18370 BaseAddr = ObAddr + *((*ObAddr) - Offset)
18371
18372 This is specific to the V3 ABI, of course. */
18373
18374 dw_loc_descr_ref tmp;
18375
18376 /* Make a copy of the object address. */
18377 tmp = new_loc_descr (DW_OP_dup, 0, 0);
18378 add_loc_descr (&loc_descr, tmp);
18379
18380 /* Extract the vtable address. */
18381 tmp = new_loc_descr (DW_OP_deref, 0, 0);
18382 add_loc_descr (&loc_descr, tmp);
18383
18384 /* Calculate the address of the offset. */
18385 offset = tree_to_shwi (BINFO_VPTR_FIELD (decl));
18386 gcc_assert (offset < 0);
18387
18388 tmp = int_loc_descriptor (-offset);
18389 add_loc_descr (&loc_descr, tmp);
18390 tmp = new_loc_descr (DW_OP_minus, 0, 0);
18391 add_loc_descr (&loc_descr, tmp);
18392
18393 /* Extract the offset. */
18394 tmp = new_loc_descr (DW_OP_deref, 0, 0);
18395 add_loc_descr (&loc_descr, tmp);
18396
18397 /* Add it to the object address. */
18398 tmp = new_loc_descr (DW_OP_plus, 0, 0);
18399 add_loc_descr (&loc_descr, tmp);
18400 }
18401 else
18402 offset = tree_to_shwi (BINFO_OFFSET (decl));
18403 }
18404 else
18405 {
18406 loc_descr = field_byte_offset (decl, ctx, &offset);
18407
18408 /* If loc_descr is available then we know the field offset is dynamic.
18409 However, GDB does not handle dynamic field offsets very well at the
18410 moment. */
18411 if (loc_descr != NULL && gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL)
18412 {
18413 loc_descr = NULL;
18414 offset = 0;
18415 }
18416
18417 /* Data member location evalutation starts with the base address on the
18418 stack. Compute the field offset and add it to this base address. */
18419 else if (loc_descr != NULL)
18420 add_loc_descr (&loc_descr, new_loc_descr (DW_OP_plus, 0, 0));
18421 }
18422
18423 if (! loc_descr)
18424 {
18425 /* While DW_AT_data_bit_offset has been added already in DWARF4,
18426 e.g. GDB only added support to it in November 2016. For DWARF5
18427 we need newer debug info consumers anyway. We might change this
18428 to dwarf_version >= 4 once most consumers catched up. */
18429 if (dwarf_version >= 5
18430 && TREE_CODE (decl) == FIELD_DECL
18431 && DECL_BIT_FIELD_TYPE (decl))
18432 {
18433 tree off = bit_position (decl);
18434 if (tree_fits_uhwi_p (off) && get_AT (die, DW_AT_bit_size))
18435 {
18436 remove_AT (die, DW_AT_byte_size);
18437 remove_AT (die, DW_AT_bit_offset);
18438 add_AT_unsigned (die, DW_AT_data_bit_offset, tree_to_uhwi (off));
18439 return;
18440 }
18441 }
18442 if (dwarf_version > 2)
18443 {
18444 /* Don't need to output a location expression, just the constant. */
18445 if (offset < 0)
18446 add_AT_int (die, DW_AT_data_member_location, offset);
18447 else
18448 add_AT_unsigned (die, DW_AT_data_member_location, offset);
18449 return;
18450 }
18451 else
18452 {
18453 enum dwarf_location_atom op;
18454
18455 /* The DWARF2 standard says that we should assume that the structure
18456 address is already on the stack, so we can specify a structure
18457 field address by using DW_OP_plus_uconst. */
18458 op = DW_OP_plus_uconst;
18459 loc_descr = new_loc_descr (op, offset, 0);
18460 }
18461 }
18462
18463 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
18464}
18465
18466/* Writes integer values to dw_vec_const array. */
18467
18468static void
18469insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
18470{
18471 while (size != 0)
18472 {
18473 *dest++ = val & 0xff;
18474 val >>= 8;
18475 --size;
18476 }
18477}
18478
18479/* Reads integers from dw_vec_const array. Inverse of insert_int. */
18480
18481static HOST_WIDE_INT
18482extract_int (const unsigned char *src, unsigned int size)
18483{
18484 HOST_WIDE_INT val = 0;
18485
18486 src += size;
18487 while (size != 0)
18488 {
18489 val <<= 8;
18490 val |= *--src & 0xff;
18491 --size;
18492 }
18493 return val;
18494}
18495
18496/* Writes wide_int values to dw_vec_const array. */
18497
18498static void
18499insert_wide_int (const wide_int &val, unsigned char *dest, int elt_size)
18500{
18501 int i;
18502
18503 if (elt_size <= HOST_BITS_PER_WIDE_INT/BITS_PER_UNIT)
18504 {
18505 insert_int ((HOST_WIDE_INT) val.elt (0), elt_size, dest);
18506 return;
18507 }
18508
18509 /* We'd have to extend this code to support odd sizes. */
18510 gcc_assert (elt_size % (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT) == 0);
18511
18512 int n = elt_size / (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
18513
18514 if (WORDS_BIG_ENDIAN)
18515 for (i = n - 1; i >= 0; i--)
18516 {
18517 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
18518 dest += sizeof (HOST_WIDE_INT);
18519 }
18520 else
18521 for (i = 0; i < n; i++)
18522 {
18523 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
18524 dest += sizeof (HOST_WIDE_INT);
18525 }
18526}
18527
18528/* Writes floating point values to dw_vec_const array. */
18529
18530static void
18531insert_float (const_rtx rtl, unsigned char *array)
18532{
18533 long val[4];
18534 int i;
18535
18536 real_to_target (val, CONST_DOUBLE_REAL_VALUE (rtl), GET_MODE (rtl));
18537
18538 /* real_to_target puts 32-bit pieces in each long. Pack them. */
18539 for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
18540 {
18541 insert_int (val[i], 4, array);
18542 array += 4;
18543 }
18544}
18545
18546/* Attach a DW_AT_const_value attribute for a variable or a parameter which
18547 does not have a "location" either in memory or in a register. These
18548 things can arise in GNU C when a constant is passed as an actual parameter
18549 to an inlined function. They can also arise in C++ where declared
18550 constants do not necessarily get memory "homes". */
18551
18552static bool
18553add_const_value_attribute (dw_die_ref die, rtx rtl)
18554{
18555 switch (GET_CODE (rtl))
18556 {
18557 case CONST_INT:
18558 {
18559 HOST_WIDE_INT val = INTVAL (rtl);
18560
18561 if (val < 0)
18562 add_AT_int (die, DW_AT_const_value, val);
18563 else
18564 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
18565 }
18566 return true;
18567
18568 case CONST_WIDE_INT:
18569 {
18570 wide_int w1 = rtx_mode_t (rtl, MAX_MODE_INT);
18571 unsigned int prec = MIN (wi::min_precision (w1, UNSIGNED),
18572 (unsigned int)CONST_WIDE_INT_NUNITS (rtl) * HOST_BITS_PER_WIDE_INT);
18573 wide_int w = wi::zext (w1, prec);
18574 add_AT_wide (die, DW_AT_const_value, w);
18575 }
18576 return true;
18577
18578 case CONST_DOUBLE:
18579 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
18580 floating-point constant. A CONST_DOUBLE is used whenever the
18581 constant requires more than one word in order to be adequately
18582 represented. */
18583 {
18584 machine_mode mode = GET_MODE (rtl);
18585
18586 if (TARGET_SUPPORTS_WIDE_INT == 0 && !SCALAR_FLOAT_MODE_P (mode))
18587 add_AT_double (die, DW_AT_const_value,
18588 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
18589 else
18590 {
18591 unsigned int length = GET_MODE_SIZE (mode);
18592 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
18593
18594 insert_float (rtl, array);
18595 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
18596 }
18597 }
18598 return true;
18599
18600 case CONST_VECTOR:
18601 {
18602 machine_mode mode = GET_MODE (rtl);
18603 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
18604 unsigned int length = CONST_VECTOR_NUNITS (rtl);
18605 unsigned char *array
18606 = ggc_vec_alloc<unsigned char> (length * elt_size);
18607 unsigned int i;
18608 unsigned char *p;
18609 machine_mode imode = GET_MODE_INNER (mode);
18610
18611 switch (GET_MODE_CLASS (mode))
18612 {
18613 case MODE_VECTOR_INT:
18614 for (i = 0, p = array; i < length; i++, p += elt_size)
18615 {
18616 rtx elt = CONST_VECTOR_ELT (rtl, i);
18617 insert_wide_int (rtx_mode_t (elt, imode), p, elt_size);
18618 }
18619 break;
18620
18621 case MODE_VECTOR_FLOAT:
18622 for (i = 0, p = array; i < length; i++, p += elt_size)
18623 {
18624 rtx elt = CONST_VECTOR_ELT (rtl, i);
18625 insert_float (elt, p);
18626 }
18627 break;
18628
18629 default:
18630 gcc_unreachable ();
18631 }
18632
18633 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
18634 }
18635 return true;
18636
18637 case CONST_STRING:
18638 if (dwarf_version >= 4 || !dwarf_strict)
18639 {
18640 dw_loc_descr_ref loc_result;
18641 resolve_one_addr (&rtl);
18642 rtl_addr:
18643 loc_result = new_addr_loc_descr (rtl, dtprel_false);
18644 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
18645 add_AT_loc (die, DW_AT_location, loc_result);
18646 vec_safe_push (used_rtx_array, rtl);
18647 return true;
18648 }
18649 return false;
18650
18651 case CONST:
18652 if (CONSTANT_P (XEXP (rtl, 0)))
18653 return add_const_value_attribute (die, XEXP (rtl, 0));
18654 /* FALLTHROUGH */
18655 case SYMBOL_REF:
18656 if (!const_ok_for_output (rtl))
18657 return false;
18658 /* FALLTHROUGH */
18659 case LABEL_REF:
18660 if (dwarf_version >= 4 || !dwarf_strict)
18661 goto rtl_addr;
18662 return false;
18663
18664 case PLUS:
18665 /* In cases where an inlined instance of an inline function is passed
18666 the address of an `auto' variable (which is local to the caller) we
18667 can get a situation where the DECL_RTL of the artificial local
18668 variable (for the inlining) which acts as a stand-in for the
18669 corresponding formal parameter (of the inline function) will look
18670 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
18671 exactly a compile-time constant expression, but it isn't the address
18672 of the (artificial) local variable either. Rather, it represents the
18673 *value* which the artificial local variable always has during its
18674 lifetime. We currently have no way to represent such quasi-constant
18675 values in Dwarf, so for now we just punt and generate nothing. */
18676 return false;
18677
18678 case HIGH:
18679 case CONST_FIXED:
18680 return false;
18681
18682 case MEM:
18683 if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
18684 && MEM_READONLY_P (rtl)
18685 && GET_MODE (rtl) == BLKmode)
18686 {
18687 add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
18688 return true;
18689 }
18690 return false;
18691
18692 default:
18693 /* No other kinds of rtx should be possible here. */
18694 gcc_unreachable ();
18695 }
18696 return false;
18697}
18698
18699/* Determine whether the evaluation of EXPR references any variables
18700 or functions which aren't otherwise used (and therefore may not be
18701 output). */
18702static tree
18703reference_to_unused (tree * tp, int * walk_subtrees,
18704 void * data ATTRIBUTE_UNUSED)
18705{
18706 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
18707 *walk_subtrees = 0;
18708
18709 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
18710 && ! TREE_ASM_WRITTEN (*tp))
18711 return *tp;
18712 /* ??? The C++ FE emits debug information for using decls, so
18713 putting gcc_unreachable here falls over. See PR31899. For now
18714 be conservative. */
18715 else if (!symtab->global_info_ready && VAR_OR_FUNCTION_DECL_P (*tp))
18716 return *tp;
18717 else if (VAR_P (*tp))
18718 {
18719 varpool_node *node = varpool_node::get (*tp);
18720 if (!node || !node->definition)
18721 return *tp;
18722 }
18723 else if (TREE_CODE (*tp) == FUNCTION_DECL
18724 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
18725 {
18726 /* The call graph machinery must have finished analyzing,
18727 optimizing and gimplifying the CU by now.
18728 So if *TP has no call graph node associated
18729 to it, it means *TP will not be emitted. */
18730 if (!cgraph_node::get (*tp))
18731 return *tp;
18732 }
18733 else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
18734 return *tp;
18735
18736 return NULL_TREE;
18737}
18738
18739/* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
18740 for use in a later add_const_value_attribute call. */
18741
18742static rtx
18743rtl_for_decl_init (tree init, tree type)
18744{
18745 rtx rtl = NULL_RTX;
18746
18747 STRIP_NOPS (init);
18748
18749 /* If a variable is initialized with a string constant without embedded
18750 zeros, build CONST_STRING. */
18751 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
18752 {
18753 tree enttype = TREE_TYPE (type);
18754 tree domain = TYPE_DOMAIN (type);
18755 machine_mode mode = TYPE_MODE (enttype);
18756
18757 if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
18758 && domain
18759 && TYPE_MAX_VALUE (domain)
18760 && TREE_CODE (TYPE_MAX_VALUE (domain)) == INTEGER_CST
18761 && integer_zerop (TYPE_MIN_VALUE (domain))
18762 && compare_tree_int (TYPE_MAX_VALUE (domain),
18763 TREE_STRING_LENGTH (init) - 1) == 0
18764 && ((size_t) TREE_STRING_LENGTH (init)
18765 == strlen (TREE_STRING_POINTER (init)) + 1))
18766 {
18767 rtl = gen_rtx_CONST_STRING (VOIDmode,
18768 ggc_strdup (TREE_STRING_POINTER (init)));
18769 rtl = gen_rtx_MEM (BLKmode, rtl);
18770 MEM_READONLY_P (rtl) = 1;
18771 }
18772 }
18773 /* Other aggregates, and complex values, could be represented using
18774 CONCAT: FIXME! */
18775 else if (AGGREGATE_TYPE_P (type)
18776 || (TREE_CODE (init) == VIEW_CONVERT_EXPR
18777 && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
18778 || TREE_CODE (type) == COMPLEX_TYPE)
18779 ;
18780 /* Vectors only work if their mode is supported by the target.
18781 FIXME: generic vectors ought to work too. */
18782 else if (TREE_CODE (type) == VECTOR_TYPE
18783 && !VECTOR_MODE_P (TYPE_MODE (type)))
18784 ;
18785 /* If the initializer is something that we know will expand into an
18786 immediate RTL constant, expand it now. We must be careful not to
18787 reference variables which won't be output. */
18788 else if (initializer_constant_valid_p (init, type)
18789 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
18790 {
18791 /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
18792 possible. */
18793 if (TREE_CODE (type) == VECTOR_TYPE)
18794 switch (TREE_CODE (init))
18795 {
18796 case VECTOR_CST:
18797 break;
18798 case CONSTRUCTOR:
18799 if (TREE_CONSTANT (init))
18800 {
18801 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (init);
18802 bool constant_p = true;
18803 tree value;
18804 unsigned HOST_WIDE_INT ix;
18805
18806 /* Even when ctor is constant, it might contain non-*_CST
18807 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
18808 belong into VECTOR_CST nodes. */
18809 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
18810 if (!CONSTANT_CLASS_P (value))
18811 {
18812 constant_p = false;
18813 break;
18814 }
18815
18816 if (constant_p)
18817 {
18818 init = build_vector_from_ctor (type, elts);
18819 break;
18820 }
18821 }
18822 /* FALLTHRU */
18823
18824 default:
18825 return NULL;
18826 }
18827
18828 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
18829
18830 /* If expand_expr returns a MEM, it wasn't immediate. */
18831 gcc_assert (!rtl || !MEM_P (rtl));
18832 }
18833
18834 return rtl;
18835}
18836
18837/* Generate RTL for the variable DECL to represent its location. */
18838
18839static rtx
18840rtl_for_decl_location (tree decl)
18841{
18842 rtx rtl;
18843
18844 /* Here we have to decide where we are going to say the parameter "lives"
18845 (as far as the debugger is concerned). We only have a couple of
18846 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
18847
18848 DECL_RTL normally indicates where the parameter lives during most of the
18849 activation of the function. If optimization is enabled however, this
18850 could be either NULL or else a pseudo-reg. Both of those cases indicate
18851 that the parameter doesn't really live anywhere (as far as the code
18852 generation parts of GCC are concerned) during most of the function's
18853 activation. That will happen (for example) if the parameter is never
18854 referenced within the function.
18855
18856 We could just generate a location descriptor here for all non-NULL
18857 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
18858 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
18859 where DECL_RTL is NULL or is a pseudo-reg.
18860
18861 Note however that we can only get away with using DECL_INCOMING_RTL as
18862 a backup substitute for DECL_RTL in certain limited cases. In cases
18863 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
18864 we can be sure that the parameter was passed using the same type as it is
18865 declared to have within the function, and that its DECL_INCOMING_RTL
18866 points us to a place where a value of that type is passed.
18867
18868 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
18869 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
18870 because in these cases DECL_INCOMING_RTL points us to a value of some
18871 type which is *different* from the type of the parameter itself. Thus,
18872 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
18873 such cases, the debugger would end up (for example) trying to fetch a
18874 `float' from a place which actually contains the first part of a
18875 `double'. That would lead to really incorrect and confusing
18876 output at debug-time.
18877
18878 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
18879 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
18880 are a couple of exceptions however. On little-endian machines we can
18881 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
18882 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
18883 an integral type that is smaller than TREE_TYPE (decl). These cases arise
18884 when (on a little-endian machine) a non-prototyped function has a
18885 parameter declared to be of type `short' or `char'. In such cases,
18886 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
18887 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
18888 passed `int' value. If the debugger then uses that address to fetch
18889 a `short' or a `char' (on a little-endian machine) the result will be
18890 the correct data, so we allow for such exceptional cases below.
18891
18892 Note that our goal here is to describe the place where the given formal
18893 parameter lives during most of the function's activation (i.e. between the
18894 end of the prologue and the start of the epilogue). We'll do that as best
18895 as we can. Note however that if the given formal parameter is modified
18896 sometime during the execution of the function, then a stack backtrace (at
18897 debug-time) will show the function as having been called with the *new*
18898 value rather than the value which was originally passed in. This happens
18899 rarely enough that it is not a major problem, but it *is* a problem, and
18900 I'd like to fix it.
18901
18902 A future version of dwarf2out.c may generate two additional attributes for
18903 any given DW_TAG_formal_parameter DIE which will describe the "passed
18904 type" and the "passed location" for the given formal parameter in addition
18905 to the attributes we now generate to indicate the "declared type" and the
18906 "active location" for each parameter. This additional set of attributes
18907 could be used by debuggers for stack backtraces. Separately, note that
18908 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
18909 This happens (for example) for inlined-instances of inline function formal
18910 parameters which are never referenced. This really shouldn't be
18911 happening. All PARM_DECL nodes should get valid non-NULL
18912 DECL_INCOMING_RTL values. FIXME. */
18913
18914 /* Use DECL_RTL as the "location" unless we find something better. */
18915 rtl = DECL_RTL_IF_SET (decl);
18916
18917 /* When generating abstract instances, ignore everything except
18918 constants, symbols living in memory, and symbols living in
18919 fixed registers. */
18920 if (! reload_completed)
18921 {
18922 if (rtl
18923 && (CONSTANT_P (rtl)
18924 || (MEM_P (rtl)
18925 && CONSTANT_P (XEXP (rtl, 0)))
18926 || (REG_P (rtl)
18927 && VAR_P (decl)
18928 && TREE_STATIC (decl))))
18929 {
18930 rtl = targetm.delegitimize_address (rtl);
18931 return rtl;
18932 }
18933 rtl = NULL_RTX;
18934 }
18935 else if (TREE_CODE (decl) == PARM_DECL)
18936 {
18937 if (rtl == NULL_RTX
18938 || is_pseudo_reg (rtl)
18939 || (MEM_P (rtl)
18940 && is_pseudo_reg (XEXP (rtl, 0))
18941 && DECL_INCOMING_RTL (decl)
18942 && MEM_P (DECL_INCOMING_RTL (decl))
18943 && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
18944 {
18945 tree declared_type = TREE_TYPE (decl);
18946 tree passed_type = DECL_ARG_TYPE (decl);
18947 machine_mode dmode = TYPE_MODE (declared_type);
18948 machine_mode pmode = TYPE_MODE (passed_type);
18949
18950 /* This decl represents a formal parameter which was optimized out.
18951 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
18952 all cases where (rtl == NULL_RTX) just below. */
18953 if (dmode == pmode)
18954 rtl = DECL_INCOMING_RTL (decl);
18955 else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
18956 && SCALAR_INT_MODE_P (dmode)
18957 && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
18958 && DECL_INCOMING_RTL (decl))
18959 {
18960 rtx inc = DECL_INCOMING_RTL (decl);
18961 if (REG_P (inc))
18962 rtl = inc;
18963 else if (MEM_P (inc))
18964 {
18965 if (BYTES_BIG_ENDIAN)
18966 rtl = adjust_address_nv (inc, dmode,
18967 GET_MODE_SIZE (pmode)
18968 - GET_MODE_SIZE (dmode));
18969 else
18970 rtl = inc;
18971 }
18972 }
18973 }
18974
18975 /* If the parm was passed in registers, but lives on the stack, then
18976 make a big endian correction if the mode of the type of the
18977 parameter is not the same as the mode of the rtl. */
18978 /* ??? This is the same series of checks that are made in dbxout.c before
18979 we reach the big endian correction code there. It isn't clear if all
18980 of these checks are necessary here, but keeping them all is the safe
18981 thing to do. */
18982 else if (MEM_P (rtl)
18983 && XEXP (rtl, 0) != const0_rtx
18984 && ! CONSTANT_P (XEXP (rtl, 0))
18985 /* Not passed in memory. */
18986 && !MEM_P (DECL_INCOMING_RTL (decl))
18987 /* Not passed by invisible reference. */
18988 && (!REG_P (XEXP (rtl, 0))
18989 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
18990 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
18991#if !HARD_FRAME_POINTER_IS_ARG_POINTER
18992 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
18993#endif
18994 )
18995 /* Big endian correction check. */
18996 && BYTES_BIG_ENDIAN
18997 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
18998 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
18999 < UNITS_PER_WORD))
19000 {
19001 machine_mode addr_mode = get_address_mode (rtl);
19002 int offset = (UNITS_PER_WORD
19003 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
19004
19005 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
19006 plus_constant (addr_mode, XEXP (rtl, 0), offset));
19007 }
19008 }
19009 else if (VAR_P (decl)
19010 && rtl
19011 && MEM_P (rtl)
19012 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
19013 && BYTES_BIG_ENDIAN)
19014 {
19015 machine_mode addr_mode = get_address_mode (rtl);
19016 int rsize = GET_MODE_SIZE (GET_MODE (rtl));
19017 int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
19018
19019 /* If a variable is declared "register" yet is smaller than
19020 a register, then if we store the variable to memory, it
19021 looks like we're storing a register-sized value, when in
19022 fact we are not. We need to adjust the offset of the
19023 storage location to reflect the actual value's bytes,
19024 else gdb will not be able to display it. */
19025 if (rsize > dsize)
19026 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
19027 plus_constant (addr_mode, XEXP (rtl, 0),
19028 rsize - dsize));
19029 }
19030
19031 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
19032 and will have been substituted directly into all expressions that use it.
19033 C does not have such a concept, but C++ and other languages do. */
19034 if (!rtl && VAR_P (decl) && DECL_INITIAL (decl))
19035 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
19036
19037 if (rtl)
19038 rtl = targetm.delegitimize_address (rtl);
19039
19040 /* If we don't look past the constant pool, we risk emitting a
19041 reference to a constant pool entry that isn't referenced from
19042 code, and thus is not emitted. */
19043 if (rtl)
19044 rtl = avoid_constant_pool_reference (rtl);
19045
19046 /* Try harder to get a rtl. If this symbol ends up not being emitted
19047 in the current CU, resolve_addr will remove the expression referencing
19048 it. */
19049 if (rtl == NULL_RTX
19050 && VAR_P (decl)
19051 && !DECL_EXTERNAL (decl)
19052 && TREE_STATIC (decl)
19053 && DECL_NAME (decl)
19054 && !DECL_HARD_REGISTER (decl)
19055 && DECL_MODE (decl) != VOIDmode)
19056 {
19057 rtl = make_decl_rtl_for_debug (decl);
19058 if (!MEM_P (rtl)
19059 || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
19060 || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
19061 rtl = NULL_RTX;
19062 }
19063
19064 return rtl;
19065}
19066
19067/* Check whether decl is a Fortran COMMON symbol. If not, NULL_TREE is
19068 returned. If so, the decl for the COMMON block is returned, and the
19069 value is the offset into the common block for the symbol. */
19070
19071static tree
19072fortran_common (tree decl, HOST_WIDE_INT *value)
19073{
19074 tree val_expr, cvar;
19075 machine_mode mode;
19076 HOST_WIDE_INT bitsize, bitpos;
19077 tree offset;
19078 int unsignedp, reversep, volatilep = 0;
19079
19080 /* If the decl isn't a VAR_DECL, or if it isn't static, or if
19081 it does not have a value (the offset into the common area), or if it
19082 is thread local (as opposed to global) then it isn't common, and shouldn't
19083 be handled as such. */
19084 if (!VAR_P (decl)
19085 || !TREE_STATIC (decl)
19086 || !DECL_HAS_VALUE_EXPR_P (decl)
19087 || !is_fortran ())
19088 return NULL_TREE;
19089
19090 val_expr = DECL_VALUE_EXPR (decl);
19091 if (TREE_CODE (val_expr) != COMPONENT_REF)
19092 return NULL_TREE;
19093
19094 cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset, &mode,
19095 &unsignedp, &reversep, &volatilep);
19096
19097 if (cvar == NULL_TREE
19098 || !VAR_P (cvar)
19099 || DECL_ARTIFICIAL (cvar)
19100 || !TREE_PUBLIC (cvar))
19101 return NULL_TREE;
19102
19103 *value = 0;
19104 if (offset != NULL)
19105 {
19106 if (!tree_fits_shwi_p (offset))
19107 return NULL_TREE;
19108 *value = tree_to_shwi (offset);
19109 }
19110 if (bitpos != 0)
19111 *value += bitpos / BITS_PER_UNIT;
19112
19113 return cvar;
19114}
19115
19116/* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
19117 data attribute for a variable or a parameter. We generate the
19118 DW_AT_const_value attribute only in those cases where the given variable
19119 or parameter does not have a true "location" either in memory or in a
19120 register. This can happen (for example) when a constant is passed as an
19121 actual argument in a call to an inline function. (It's possible that
19122 these things can crop up in other ways also.) Note that one type of
19123 constant value which can be passed into an inlined function is a constant
19124 pointer. This can happen for example if an actual argument in an inlined
19125 function call evaluates to a compile-time constant address.
19126
19127 CACHE_P is true if it is worth caching the location list for DECL,
19128 so that future calls can reuse it rather than regenerate it from scratch.
19129 This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
19130 since we will need to refer to them each time the function is inlined. */
19131
19132static bool
19133add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p)
19134{
19135 rtx rtl;
19136 dw_loc_list_ref list;
19137 var_loc_list *loc_list;
19138 cached_dw_loc_list *cache;
19139
19140 if (early_dwarf)
19141 return false;
19142
19143 if (TREE_CODE (decl) == ERROR_MARK)
19144 return false;
19145
19146 if (get_AT (die, DW_AT_location)
19147 || get_AT (die, DW_AT_const_value))
19148 return true;
19149
19150 gcc_assert (VAR_P (decl) || TREE_CODE (decl) == PARM_DECL
19151 || TREE_CODE (decl) == RESULT_DECL);
19152
19153 /* Try to get some constant RTL for this decl, and use that as the value of
19154 the location. */
19155
19156 rtl = rtl_for_decl_location (decl);
19157 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
19158 && add_const_value_attribute (die, rtl))
19159 return true;
19160
19161 /* See if we have single element location list that is equivalent to
19162 a constant value. That way we are better to use add_const_value_attribute
19163 rather than expanding constant value equivalent. */
19164 loc_list = lookup_decl_loc (decl);
19165 if (loc_list
19166 && loc_list->first
19167 && loc_list->first->next == NULL
19168 && NOTE_P (loc_list->first->loc)
19169 && NOTE_VAR_LOCATION (loc_list->first->loc)
19170 && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
19171 {
19172 struct var_loc_node *node;
19173
19174 node = loc_list->first;
19175 rtl = NOTE_VAR_LOCATION_LOC (node->loc);
19176 if (GET_CODE (rtl) == EXPR_LIST)
19177 rtl = XEXP (rtl, 0);
19178 if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
19179 && add_const_value_attribute (die, rtl))
19180 return true;
19181 }
19182 /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
19183 list several times. See if we've already cached the contents. */
19184 list = NULL;
19185 if (loc_list == NULL || cached_dw_loc_list_table == NULL)
19186 cache_p = false;
19187 if (cache_p)
19188 {
19189 cache = cached_dw_loc_list_table->find_with_hash (decl, DECL_UID (decl));
19190 if (cache)
19191 list = cache->loc_list;
19192 }
19193 if (list == NULL)
19194 {
19195 list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2,
19196 NULL);
19197 /* It is usually worth caching this result if the decl is from
19198 BLOCK_NONLOCALIZED_VARS and if the list has at least two elements. */
19199 if (cache_p && list && list->dw_loc_next)
19200 {
19201 cached_dw_loc_list **slot
19202 = cached_dw_loc_list_table->find_slot_with_hash (decl,
19203 DECL_UID (decl),
19204 INSERT);
19205 cache = ggc_cleared_alloc<cached_dw_loc_list> ();
19206 cache->decl_id = DECL_UID (decl);
19207 cache->loc_list = list;
19208 *slot = cache;
19209 }
19210 }
19211 if (list)
19212 {
19213 add_AT_location_description (die, DW_AT_location, list);
19214 return true;
19215 }
19216 /* None of that worked, so it must not really have a location;
19217 try adding a constant value attribute from the DECL_INITIAL. */
19218 return tree_add_const_value_attribute_for_decl (die, decl);
19219}
19220
19221/* Helper function for tree_add_const_value_attribute. Natively encode
19222 initializer INIT into an array. Return true if successful. */
19223
19224static bool
19225native_encode_initializer (tree init, unsigned char *array, int size)
19226{
19227 tree type;
19228
19229 if (init == NULL_TREE)
19230 return false;
19231
19232 STRIP_NOPS (init);
19233 switch (TREE_CODE (init))
19234 {
19235 case STRING_CST:
19236 type = TREE_TYPE (init);
19237 if (TREE_CODE (type) == ARRAY_TYPE)
19238 {
19239 tree enttype = TREE_TYPE (type);
19240 machine_mode mode = TYPE_MODE (enttype);
19241
19242 if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
19243 return false;
19244 if (int_size_in_bytes (type) != size)
19245 return false;
19246 if (size > TREE_STRING_LENGTH (init))
19247 {
19248 memcpy (array, TREE_STRING_POINTER (init),
19249 TREE_STRING_LENGTH (init));
19250 memset (array + TREE_STRING_LENGTH (init),
19251 '\0', size - TREE_STRING_LENGTH (init));
19252 }
19253 else
19254 memcpy (array, TREE_STRING_POINTER (init), size);
19255 return true;
19256 }
19257 return false;
19258 case CONSTRUCTOR:
19259 type = TREE_TYPE (init);
19260 if (int_size_in_bytes (type) != size)
19261 return false;
19262 if (TREE_CODE (type) == ARRAY_TYPE)
19263 {
19264 HOST_WIDE_INT min_index;
19265 unsigned HOST_WIDE_INT cnt;
19266 int curpos = 0, fieldsize;
19267 constructor_elt *ce;
19268
19269 if (TYPE_DOMAIN (type) == NULL_TREE
19270 || !tree_fits_shwi_p (TYPE_MIN_VALUE (TYPE_DOMAIN (type))))
19271 return false;
19272
19273 fieldsize = int_size_in_bytes (TREE_TYPE (type));
19274 if (fieldsize <= 0)
19275 return false;
19276
19277 min_index = tree_to_shwi (TYPE_MIN_VALUE (TYPE_DOMAIN (type)));
19278 memset (array, '\0', size);
19279 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
19280 {
19281 tree val = ce->value;
19282 tree index = ce->index;
19283 int pos = curpos;
19284 if (index && TREE_CODE (index) == RANGE_EXPR)
19285 pos = (tree_to_shwi (TREE_OPERAND (index, 0)) - min_index)
19286 * fieldsize;
19287 else if (index)
19288 pos = (tree_to_shwi (index) - min_index) * fieldsize;
19289
19290 if (val)
19291 {
19292 STRIP_NOPS (val);
19293 if (!native_encode_initializer (val, array + pos, fieldsize))
19294 return false;
19295 }
19296 curpos = pos + fieldsize;
19297 if (index && TREE_CODE (index) == RANGE_EXPR)
19298 {
19299 int count = tree_to_shwi (TREE_OPERAND (index, 1))
19300 - tree_to_shwi (TREE_OPERAND (index, 0));
19301 while (count-- > 0)
19302 {
19303 if (val)
19304 memcpy (array + curpos, array + pos, fieldsize);
19305 curpos += fieldsize;
19306 }
19307 }
19308 gcc_assert (curpos <= size);
19309 }
19310 return true;
19311 }
19312 else if (TREE_CODE (type) == RECORD_TYPE
19313 || TREE_CODE (type) == UNION_TYPE)
19314 {
19315 tree field = NULL_TREE;
19316 unsigned HOST_WIDE_INT cnt;
19317 constructor_elt *ce;
19318
19319 if (int_size_in_bytes (type) != size)
19320 return false;
19321
19322 if (TREE_CODE (type) == RECORD_TYPE)
19323 field = TYPE_FIELDS (type);
19324
19325 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
19326 {
19327 tree val = ce->value;
19328 int pos, fieldsize;
19329
19330 if (ce->index != 0)
19331 field = ce->index;
19332
19333 if (val)
19334 STRIP_NOPS (val);
19335
19336 if (field == NULL_TREE || DECL_BIT_FIELD (field))
19337 return false;
19338
19339 if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
19340 && TYPE_DOMAIN (TREE_TYPE (field))
19341 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
19342 return false;
19343 else if (DECL_SIZE_UNIT (field) == NULL_TREE
19344 || !tree_fits_shwi_p (DECL_SIZE_UNIT (field)))
19345 return false;
19346 fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
19347 pos = int_byte_position (field);
19348 gcc_assert (pos + fieldsize <= size);
19349 if (val && fieldsize != 0
19350 && !native_encode_initializer (val, array + pos, fieldsize))
19351 return false;
19352 }
19353 return true;
19354 }
19355 return false;
19356 case VIEW_CONVERT_EXPR:
19357 case NON_LVALUE_EXPR:
19358 return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
19359 default:
19360 return native_encode_expr (init, array, size) == size;
19361 }
19362}
19363
19364/* Attach a DW_AT_const_value attribute to DIE. The value of the
19365 attribute is the const value T. */
19366
19367static bool
19368tree_add_const_value_attribute (dw_die_ref die, tree t)
19369{
19370 tree init;
19371 tree type = TREE_TYPE (t);
19372 rtx rtl;
19373
19374 if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
19375 return false;
19376
19377 init = t;
19378 gcc_assert (!DECL_P (init));
19379
19380 if (! early_dwarf)
19381 {
19382 rtl = rtl_for_decl_init (init, type);
19383 if (rtl)
19384 return add_const_value_attribute (die, rtl);
19385 }
19386 /* If the host and target are sane, try harder. */
19387 if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
19388 && initializer_constant_valid_p (init, type))
19389 {
19390 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
19391 if (size > 0 && (int) size == size)
19392 {
19393 unsigned char *array = ggc_cleared_vec_alloc<unsigned char> (size);
19394
19395 if (native_encode_initializer (init, array, size))
19396 {
19397 add_AT_vec (die, DW_AT_const_value, size, 1, array);
19398 return true;
19399 }
19400 ggc_free (array);
19401 }
19402 }
19403 return false;
19404}
19405
19406/* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
19407 attribute is the const value of T, where T is an integral constant
19408 variable with static storage duration
19409 (so it can't be a PARM_DECL or a RESULT_DECL). */
19410
19411static bool
19412tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
19413{
19414
19415 if (!decl
19416 || (!VAR_P (decl) && TREE_CODE (decl) != CONST_DECL)
19417 || (VAR_P (decl) && !TREE_STATIC (decl)))
19418 return false;
19419
19420 if (TREE_READONLY (decl)
19421 && ! TREE_THIS_VOLATILE (decl)
19422 && DECL_INITIAL (decl))
19423 /* OK */;
19424 else
19425 return false;
19426
19427 /* Don't add DW_AT_const_value if abstract origin already has one. */
19428 if (get_AT (var_die, DW_AT_const_value))
19429 return false;
19430
19431 return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
19432}
19433
19434/* Convert the CFI instructions for the current function into a
19435 location list. This is used for DW_AT_frame_base when we targeting
19436 a dwarf2 consumer that does not support the dwarf3
19437 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
19438 expressions. */
19439
19440static dw_loc_list_ref
19441convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
19442{
19443 int ix;
19444 dw_fde_ref fde;
19445 dw_loc_list_ref list, *list_tail;
19446 dw_cfi_ref cfi;
19447 dw_cfa_location last_cfa, next_cfa;
19448 const char *start_label, *last_label, *section;
19449 dw_cfa_location remember;
19450
19451 fde = cfun->fde;
19452 gcc_assert (fde != NULL);
19453
19454 section = secname_for_decl (current_function_decl);
19455 list_tail = &list;
19456 list = NULL;
19457
19458 memset (&next_cfa, 0, sizeof (next_cfa));
19459 next_cfa.reg = INVALID_REGNUM;
19460 remember = next_cfa;
19461
19462 start_label = fde->dw_fde_begin;
19463
19464 /* ??? Bald assumption that the CIE opcode list does not contain
19465 advance opcodes. */
19466 FOR_EACH_VEC_ELT (*cie_cfi_vec, ix, cfi)
19467 lookup_cfa_1 (cfi, &next_cfa, &remember);
19468
19469 last_cfa = next_cfa;
19470 last_label = start_label;
19471
19472 if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
19473 {
19474 /* If the first partition contained no CFI adjustments, the
19475 CIE opcodes apply to the whole first partition. */
19476 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19477 fde->dw_fde_begin, fde->dw_fde_end, section);
19478 list_tail =&(*list_tail)->dw_loc_next;
19479 start_label = last_label = fde->dw_fde_second_begin;
19480 }
19481
19482 FOR_EACH_VEC_SAFE_ELT (fde->dw_fde_cfi, ix, cfi)
19483 {
19484 switch (cfi->dw_cfi_opc)
19485 {
19486 case DW_CFA_set_loc:
19487 case DW_CFA_advance_loc1:
19488 case DW_CFA_advance_loc2:
19489 case DW_CFA_advance_loc4:
19490 if (!cfa_equal_p (&last_cfa, &next_cfa))
19491 {
19492 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19493 start_label, last_label, section);
19494
19495 list_tail = &(*list_tail)->dw_loc_next;
19496 last_cfa = next_cfa;
19497 start_label = last_label;
19498 }
19499 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
19500 break;
19501
19502 case DW_CFA_advance_loc:
19503 /* The encoding is complex enough that we should never emit this. */
19504 gcc_unreachable ();
19505
19506 default:
19507 lookup_cfa_1 (cfi, &next_cfa, &remember);
19508 break;
19509 }
19510 if (ix + 1 == fde->dw_fde_switch_cfi_index)
19511 {
19512 if (!cfa_equal_p (&last_cfa, &next_cfa))
19513 {
19514 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19515 start_label, last_label, section);
19516
19517 list_tail = &(*list_tail)->dw_loc_next;
19518 last_cfa = next_cfa;
19519 start_label = last_label;
19520 }
19521 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19522 start_label, fde->dw_fde_end, section);
19523 list_tail = &(*list_tail)->dw_loc_next;
19524 start_label = last_label = fde->dw_fde_second_begin;
19525 }
19526 }
19527
19528 if (!cfa_equal_p (&last_cfa, &next_cfa))
19529 {
19530 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19531 start_label, last_label, section);
19532 list_tail = &(*list_tail)->dw_loc_next;
19533 start_label = last_label;
19534 }
19535
19536 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
19537 start_label,
19538 fde->dw_fde_second_begin
19539 ? fde->dw_fde_second_end : fde->dw_fde_end,
19540 section);
19541
19542 if (list && list->dw_loc_next)
19543 gen_llsym (list);
19544
19545 return list;
19546}
19547
19548/* Compute a displacement from the "steady-state frame pointer" to the
19549 frame base (often the same as the CFA), and store it in
19550 frame_pointer_fb_offset. OFFSET is added to the displacement
19551 before the latter is negated. */
19552
19553static void
19554compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
19555{
19556 rtx reg, elim;
19557
19558#ifdef FRAME_POINTER_CFA_OFFSET
19559 reg = frame_pointer_rtx;
19560 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
19561#else
19562 reg = arg_pointer_rtx;
19563 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
19564#endif
19565
19566 elim = (ira_use_lra_p
19567 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
19568 : eliminate_regs (reg, VOIDmode, NULL_RTX));
19569 if (GET_CODE (elim) == PLUS)
19570 {
19571 offset += INTVAL (XEXP (elim, 1));
19572 elim = XEXP (elim, 0);
19573 }
19574
19575 frame_pointer_fb_offset = -offset;
19576
19577 /* ??? AVR doesn't set up valid eliminations when there is no stack frame
19578 in which to eliminate. This is because it's stack pointer isn't
19579 directly accessible as a register within the ISA. To work around
19580 this, assume that while we cannot provide a proper value for
19581 frame_pointer_fb_offset, we won't need one either. */
19582 frame_pointer_fb_offset_valid
19583 = ((SUPPORTS_STACK_ALIGNMENT
19584 && (elim == hard_frame_pointer_rtx
19585 || elim == stack_pointer_rtx))
19586 || elim == (frame_pointer_needed
19587 ? hard_frame_pointer_rtx
19588 : stack_pointer_rtx));
19589}
19590
19591/* Generate a DW_AT_name attribute given some string value to be included as
19592 the value of the attribute. */
19593
19594static void
19595add_name_attribute (dw_die_ref die, const char *name_string)
19596{
19597 if (name_string != NULL && *name_string != 0)
19598 {
19599 if (demangle_name_func)
19600 name_string = (*demangle_name_func) (name_string);
19601
19602 add_AT_string (die, DW_AT_name, name_string);
19603 }
19604}
19605
19606/* Retrieve the descriptive type of TYPE, if any, make sure it has a
19607 DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
19608 of TYPE accordingly.
19609
19610 ??? This is a temporary measure until after we're able to generate
19611 regular DWARF for the complex Ada type system. */
19612
19613static void
19614add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
19615 dw_die_ref context_die)
19616{
19617 tree dtype;
19618 dw_die_ref dtype_die;
19619
19620 if (!lang_hooks.types.descriptive_type)
19621 return;
19622
19623 dtype = lang_hooks.types.descriptive_type (type);
19624 if (!dtype)
19625 return;
19626
19627 dtype_die = lookup_type_die (dtype);
19628 if (!dtype_die)
19629 {
19630 gen_type_die (dtype, context_die);
19631 dtype_die = lookup_type_die (dtype);
19632 gcc_assert (dtype_die);
19633 }
19634
19635 add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
19636}
19637
19638/* Retrieve the comp_dir string suitable for use with DW_AT_comp_dir. */
19639
19640static const char *
19641comp_dir_string (void)
19642{
19643 const char *wd;
19644 char *wd1;
19645 static const char *cached_wd = NULL;
19646
19647 if (cached_wd != NULL)
19648 return cached_wd;
19649
19650 wd = get_src_pwd ();
19651 if (wd == NULL)
19652 return NULL;
19653
19654 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
19655 {
19656 int wdlen;
19657
19658 wdlen = strlen (wd);
19659 wd1 = ggc_vec_alloc<char> (wdlen + 2);
19660 strcpy (wd1, wd);
19661 wd1 [wdlen] = DIR_SEPARATOR;
19662 wd1 [wdlen + 1] = 0;
19663 wd = wd1;
19664 }
19665
19666 cached_wd = remap_debug_filename (wd);
19667 return cached_wd;
19668}
19669
19670/* Generate a DW_AT_comp_dir attribute for DIE. */
19671
19672static void
19673add_comp_dir_attribute (dw_die_ref die)
19674{
19675 const char * wd = comp_dir_string ();
19676 if (wd != NULL)
19677 add_AT_string (die, DW_AT_comp_dir, wd);
19678}
19679
19680/* Given a tree node VALUE describing a scalar attribute ATTR (i.e. a bound, a
19681 pointer computation, ...), output a representation for that bound according
19682 to the accepted FORMS (see enum dw_scalar_form) and add it to DIE. See
19683 loc_list_from_tree for the meaning of CONTEXT. */
19684
19685static void
19686add_scalar_info (dw_die_ref die, enum dwarf_attribute attr, tree value,
19687 int forms, struct loc_descr_context *context)
19688{
19689 dw_die_ref context_die, decl_die;
19690 dw_loc_list_ref list;
19691 bool strip_conversions = true;
19692 bool placeholder_seen = false;
19693
19694 while (strip_conversions)
19695 switch (TREE_CODE (value))
19696 {
19697 case ERROR_MARK:
19698 case SAVE_EXPR:
19699 return;
19700
19701 CASE_CONVERT:
19702 case VIEW_CONVERT_EXPR:
19703 value = TREE_OPERAND (value, 0);
19704 break;
19705
19706 default:
19707 strip_conversions = false;
19708 break;
19709 }
19710
19711 /* If possible and permitted, output the attribute as a constant. */
19712 if ((forms & dw_scalar_form_constant) != 0
19713 && TREE_CODE (value) == INTEGER_CST)
19714 {
19715 unsigned int prec = simple_type_size_in_bits (TREE_TYPE (value));
19716
19717 /* If HOST_WIDE_INT is big enough then represent the bound as
19718 a constant value. We need to choose a form based on
19719 whether the type is signed or unsigned. We cannot just
19720 call add_AT_unsigned if the value itself is positive
19721 (add_AT_unsigned might add the unsigned value encoded as
19722 DW_FORM_data[1248]). Some DWARF consumers will lookup the
19723 bounds type and then sign extend any unsigned values found
19724 for signed types. This is needed only for
19725 DW_AT_{lower,upper}_bound, since for most other attributes,
19726 consumers will treat DW_FORM_data[1248] as unsigned values,
19727 regardless of the underlying type. */
19728 if (prec <= HOST_BITS_PER_WIDE_INT
19729 || tree_fits_uhwi_p (value))
19730 {
19731 if (TYPE_UNSIGNED (TREE_TYPE (value)))
19732 add_AT_unsigned (die, attr, TREE_INT_CST_LOW (value));
19733 else
19734 add_AT_int (die, attr, TREE_INT_CST_LOW (value));
19735 }
19736 else
19737 /* Otherwise represent the bound as an unsigned value with
19738 the precision of its type. The precision and signedness
19739 of the type will be necessary to re-interpret it
19740 unambiguously. */
19741 add_AT_wide (die, attr, value);
19742 return;
19743 }
19744
19745 /* Otherwise, if it's possible and permitted too, output a reference to
19746 another DIE. */
19747 if ((forms & dw_scalar_form_reference) != 0)
19748 {
19749 tree decl = NULL_TREE;
19750
19751 /* Some type attributes reference an outer type. For instance, the upper
19752 bound of an array may reference an embedding record (this happens in
19753 Ada). */
19754 if (TREE_CODE (value) == COMPONENT_REF
19755 && TREE_CODE (TREE_OPERAND (value, 0)) == PLACEHOLDER_EXPR
19756 && TREE_CODE (TREE_OPERAND (value, 1)) == FIELD_DECL)
19757 decl = TREE_OPERAND (value, 1);
19758
19759 else if (VAR_P (value)
19760 || TREE_CODE (value) == PARM_DECL
19761 || TREE_CODE (value) == RESULT_DECL)
19762 decl = value;
19763
19764 if (decl != NULL_TREE)
19765 {
19766 dw_die_ref decl_die = lookup_decl_die (decl);
19767
19768 /* ??? Can this happen, or should the variable have been bound
19769 first? Probably it can, since I imagine that we try to create
19770 the types of parameters in the order in which they exist in
19771 the list, and won't have created a forward reference to a
19772 later parameter. */
19773 if (decl_die != NULL)
19774 {
19775 add_AT_die_ref (die, attr, decl_die);
19776 return;
19777 }
19778 }
19779 }
19780
19781 /* Last chance: try to create a stack operation procedure to evaluate the
19782 value. Do nothing if even that is not possible or permitted. */
19783 if ((forms & dw_scalar_form_exprloc) == 0)
19784 return;
19785
19786 list = loc_list_from_tree (value, 2, context);
19787 if (context && context->placeholder_arg)
19788 {
19789 placeholder_seen = context->placeholder_seen;
19790 context->placeholder_seen = false;
19791 }
19792 if (list == NULL || single_element_loc_list_p (list))
19793 {
19794 /* If this attribute is not a reference nor constant, it is
19795 a DWARF expression rather than location description. For that
19796 loc_list_from_tree (value, 0, &context) is needed. */
19797 dw_loc_list_ref list2 = loc_list_from_tree (value, 0, context);
19798 if (list2 && single_element_loc_list_p (list2))
19799 {
19800 if (placeholder_seen)
19801 {
19802 struct dwarf_procedure_info dpi;
19803 dpi.fndecl = NULL_TREE;
19804 dpi.args_count = 1;
19805 if (!resolve_args_picking (list2->expr, 1, &dpi))
19806 return;
19807 }
19808 add_AT_loc (die, attr, list2->expr);
19809 return;
19810 }
19811 }
19812
19813 /* If that failed to give a single element location list, fall back to
19814 outputting this as a reference... still if permitted. */
19815 if (list == NULL
19816 || (forms & dw_scalar_form_reference) == 0
19817 || placeholder_seen)
19818 return;
19819
19820 if (current_function_decl == 0)
19821 context_die = comp_unit_die ();
19822 else
19823 context_die = lookup_decl_die (current_function_decl);
19824
19825 decl_die = new_die (DW_TAG_variable, context_die, value);
19826 add_AT_flag (decl_die, DW_AT_artificial, 1);
19827 add_type_attribute (decl_die, TREE_TYPE (value), TYPE_QUAL_CONST, false,
19828 context_die);
19829 add_AT_location_description (decl_die, DW_AT_location, list);
19830 add_AT_die_ref (die, attr, decl_die);
19831}
19832
19833/* Return the default for DW_AT_lower_bound, or -1 if there is not any
19834 default. */
19835
19836static int
19837lower_bound_default (void)
19838{
19839 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
19840 {
19841 case DW_LANG_C:
19842 case DW_LANG_C89:
19843 case DW_LANG_C99:
19844 case DW_LANG_C11:
19845 case DW_LANG_C_plus_plus:
19846 case DW_LANG_C_plus_plus_11:
19847 case DW_LANG_C_plus_plus_14:
19848 case DW_LANG_ObjC:
19849 case DW_LANG_ObjC_plus_plus:
19850 case DW_LANG_Java:
19851 return 0;
19852 case DW_LANG_Fortran77:
19853 case DW_LANG_Fortran90:
19854 case DW_LANG_Fortran95:
19855 case DW_LANG_Fortran03:
19856 case DW_LANG_Fortran08:
19857 return 1;
19858 case DW_LANG_UPC:
19859 case DW_LANG_D:
19860 case DW_LANG_Python:
19861 return dwarf_version >= 4 ? 0 : -1;
19862 case DW_LANG_Ada95:
19863 case DW_LANG_Ada83:
19864 case DW_LANG_Cobol74:
19865 case DW_LANG_Cobol85:
19866 case DW_LANG_Pascal83:
19867 case DW_LANG_Modula2:
19868 case DW_LANG_PLI:
19869 return dwarf_version >= 4 ? 1 : -1;
19870 default:
19871 return -1;
19872 }
19873}
19874
19875/* Given a tree node describing an array bound (either lower or upper) output
19876 a representation for that bound. */
19877
19878static void
19879add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr,
19880 tree bound, struct loc_descr_context *context)
19881{
19882 int dflt;
19883
19884 while (1)
19885 switch (TREE_CODE (bound))
19886 {
19887 /* Strip all conversions. */
19888 CASE_CONVERT:
19889 case VIEW_CONVERT_EXPR:
19890 bound = TREE_OPERAND (bound, 0);
19891 break;
19892
19893 /* All fixed-bounds are represented by INTEGER_CST nodes. Lower bounds
19894 are even omitted when they are the default. */
19895 case INTEGER_CST:
19896 /* If the value for this bound is the default one, we can even omit the
19897 attribute. */
19898 if (bound_attr == DW_AT_lower_bound
19899 && tree_fits_shwi_p (bound)
19900 && (dflt = lower_bound_default ()) != -1
19901 && tree_to_shwi (bound) == dflt)
19902 return;
19903
19904 /* FALLTHRU */
19905
19906 default:
19907 /* Because of the complex interaction there can be with other GNAT
19908 encodings, GDB isn't ready yet to handle proper DWARF description
19909 for self-referencial subrange bounds: let GNAT encodings do the
19910 magic in such a case. */
19911 if (is_ada ()
19912 && gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL
19913 && contains_placeholder_p (bound))
19914 return;
19915
19916 add_scalar_info (subrange_die, bound_attr, bound,
19917 dw_scalar_form_constant
19918 | dw_scalar_form_exprloc
19919 | dw_scalar_form_reference,
19920 context);
19921 return;
19922 }
19923}
19924
19925/* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
19926 possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
19927 Note that the block of subscript information for an array type also
19928 includes information about the element type of the given array type.
19929
19930 This function reuses previously set type and bound information if
19931 available. */
19932
19933static void
19934add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
19935{
19936 unsigned dimension_number;
19937 tree lower, upper;
19938 dw_die_ref child = type_die->die_child;
19939
19940 for (dimension_number = 0;
19941 TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
19942 type = TREE_TYPE (type), dimension_number++)
19943 {
19944 tree domain = TYPE_DOMAIN (type);
19945
19946 if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
19947 break;
19948
19949 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
19950 and (in GNU C only) variable bounds. Handle all three forms
19951 here. */
19952
19953 /* Find and reuse a previously generated DW_TAG_subrange_type if
19954 available.
19955
19956 For multi-dimensional arrays, as we iterate through the
19957 various dimensions in the enclosing for loop above, we also
19958 iterate through the DIE children and pick at each
19959 DW_TAG_subrange_type previously generated (if available).
19960 Each child DW_TAG_subrange_type DIE describes the range of
19961 the current dimension. At this point we should have as many
19962 DW_TAG_subrange_type's as we have dimensions in the
19963 array. */
19964 dw_die_ref subrange_die = NULL;
19965 if (child)
19966 while (1)
19967 {
19968 child = child->die_sib;
19969 if (child->die_tag == DW_TAG_subrange_type)
19970 subrange_die = child;
19971 if (child == type_die->die_child)
19972 {
19973 /* If we wrapped around, stop looking next time. */
19974 child = NULL;
19975 break;
19976 }
19977 if (child->die_tag == DW_TAG_subrange_type)
19978 break;
19979 }
19980 if (!subrange_die)
19981 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
19982
19983 if (domain)
19984 {
19985 /* We have an array type with specified bounds. */
19986 lower = TYPE_MIN_VALUE (domain);
19987 upper = TYPE_MAX_VALUE (domain);
19988
19989 /* Define the index type. */
19990 if (TREE_TYPE (domain)
19991 && !get_AT (subrange_die, DW_AT_type))
19992 {
19993 /* ??? This is probably an Ada unnamed subrange type. Ignore the
19994 TREE_TYPE field. We can't emit debug info for this
19995 because it is an unnamed integral type. */
19996 if (TREE_CODE (domain) == INTEGER_TYPE
19997 && TYPE_NAME (domain) == NULL_TREE
19998 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
19999 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
20000 ;
20001 else
20002 add_type_attribute (subrange_die, TREE_TYPE (domain),
20003 TYPE_UNQUALIFIED, false, type_die);
20004 }
20005
20006 /* ??? If upper is NULL, the array has unspecified length,
20007 but it does have a lower bound. This happens with Fortran
20008 dimension arr(N:*)
20009 Since the debugger is definitely going to need to know N
20010 to produce useful results, go ahead and output the lower
20011 bound solo, and hope the debugger can cope. */
20012
20013 if (!get_AT (subrange_die, DW_AT_lower_bound))
20014 add_bound_info (subrange_die, DW_AT_lower_bound, lower, NULL);
20015 if (upper && !get_AT (subrange_die, DW_AT_upper_bound))
20016 add_bound_info (subrange_die, DW_AT_upper_bound, upper, NULL);
20017 }
20018
20019 /* Otherwise we have an array type with an unspecified length. The
20020 DWARF-2 spec does not say how to handle this; let's just leave out the
20021 bounds. */
20022 }
20023}
20024
20025/* Add a DW_AT_byte_size attribute to DIE with TREE_NODE's size. */
20026
20027static void
20028add_byte_size_attribute (dw_die_ref die, tree tree_node)
20029{
20030 dw_die_ref decl_die;
20031 HOST_WIDE_INT size;
20032 dw_loc_descr_ref size_expr = NULL;
20033
20034 switch (TREE_CODE (tree_node))
20035 {
20036 case ERROR_MARK:
20037 size = 0;
20038 break;
20039 case ENUMERAL_TYPE:
20040 case RECORD_TYPE:
20041 case UNION_TYPE:
20042 case QUAL_UNION_TYPE:
20043 if (TREE_CODE (TYPE_SIZE_UNIT (tree_node)) == VAR_DECL
20044 && (decl_die = lookup_decl_die (TYPE_SIZE_UNIT (tree_node))))
20045 {
20046 add_AT_die_ref (die, DW_AT_byte_size, decl_die);
20047 return;
20048 }
20049 size_expr = type_byte_size (tree_node, &size);
20050 break;
20051 case FIELD_DECL:
20052 /* For a data member of a struct or union, the DW_AT_byte_size is
20053 generally given as the number of bytes normally allocated for an
20054 object of the *declared* type of the member itself. This is true
20055 even for bit-fields. */
20056 size = int_size_in_bytes (field_type (tree_node));
20057 break;
20058 default:
20059 gcc_unreachable ();
20060 }
20061
20062 /* Support for dynamically-sized objects was introduced by DWARFv3.
20063 At the moment, GDB does not handle variable byte sizes very well,
20064 though. */
20065 if ((dwarf_version >= 3 || !dwarf_strict)
20066 && gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL
20067 && size_expr != NULL)
20068 add_AT_loc (die, DW_AT_byte_size, size_expr);
20069
20070 /* Note that `size' might be -1 when we get to this point. If it is, that
20071 indicates that the byte size of the entity in question is variable and
20072 that we could not generate a DWARF expression that computes it. */
20073 if (size >= 0)
20074 add_AT_unsigned (die, DW_AT_byte_size, size);
20075}
20076
20077/* Add a DW_AT_alignment attribute to DIE with TREE_NODE's non-default
20078 alignment. */
20079
20080static void
20081add_alignment_attribute (dw_die_ref die, tree tree_node)
20082{
20083 if (dwarf_version < 5 && dwarf_strict)
20084 return;
20085
20086 unsigned align;
20087
20088 if (DECL_P (tree_node))
20089 {
20090 if (!DECL_USER_ALIGN (tree_node))
20091 return;
20092
20093 align = DECL_ALIGN_UNIT (tree_node);
20094 }
20095 else if (TYPE_P (tree_node))
20096 {
20097 if (!TYPE_USER_ALIGN (tree_node))
20098 return;
20099
20100 align = TYPE_ALIGN_UNIT (tree_node);
20101 }
20102 else
20103 gcc_unreachable ();
20104
20105 add_AT_unsigned (die, DW_AT_alignment, align);
20106}
20107
20108/* For a FIELD_DECL node which represents a bit-field, output an attribute
20109 which specifies the distance in bits from the highest order bit of the
20110 "containing object" for the bit-field to the highest order bit of the
20111 bit-field itself.
20112
20113 For any given bit-field, the "containing object" is a hypothetical object
20114 (of some integral or enum type) within which the given bit-field lives. The
20115 type of this hypothetical "containing object" is always the same as the
20116 declared type of the individual bit-field itself. The determination of the
20117 exact location of the "containing object" for a bit-field is rather
20118 complicated. It's handled by the `field_byte_offset' function (above).
20119
20120 CTX is required: see the comment for VLR_CONTEXT.
20121
20122 Note that it is the size (in bytes) of the hypothetical "containing object"
20123 which will be given in the DW_AT_byte_size attribute for this bit-field.
20124 (See `byte_size_attribute' above). */
20125
20126static inline void
20127add_bit_offset_attribute (dw_die_ref die, tree decl, struct vlr_context *ctx)
20128{
20129 HOST_WIDE_INT object_offset_in_bytes;
20130 tree original_type = DECL_BIT_FIELD_TYPE (decl);
20131 HOST_WIDE_INT bitpos_int;
20132 HOST_WIDE_INT highest_order_object_bit_offset;
20133 HOST_WIDE_INT highest_order_field_bit_offset;
20134 HOST_WIDE_INT bit_offset;
20135
20136 field_byte_offset (decl, ctx, &object_offset_in_bytes);
20137
20138 /* Must be a field and a bit field. */
20139 gcc_assert (original_type && TREE_CODE (decl) == FIELD_DECL);
20140
20141 /* We can't yet handle bit-fields whose offsets are variable, so if we
20142 encounter such things, just return without generating any attribute
20143 whatsoever. Likewise for variable or too large size. */
20144 if (! tree_fits_shwi_p (bit_position (decl))
20145 || ! tree_fits_uhwi_p (DECL_SIZE (decl)))
20146 return;
20147
20148 bitpos_int = int_bit_position (decl);
20149
20150 /* Note that the bit offset is always the distance (in bits) from the
20151 highest-order bit of the "containing object" to the highest-order bit of
20152 the bit-field itself. Since the "high-order end" of any object or field
20153 is different on big-endian and little-endian machines, the computation
20154 below must take account of these differences. */
20155 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
20156 highest_order_field_bit_offset = bitpos_int;
20157
20158 if (! BYTES_BIG_ENDIAN)
20159 {
20160 highest_order_field_bit_offset += tree_to_shwi (DECL_SIZE (decl));
20161 highest_order_object_bit_offset +=
20162 simple_type_size_in_bits (original_type);
20163 }
20164
20165 bit_offset
20166 = (! BYTES_BIG_ENDIAN
20167 ? highest_order_object_bit_offset - highest_order_field_bit_offset
20168 : highest_order_field_bit_offset - highest_order_object_bit_offset);
20169
20170 if (bit_offset < 0)
20171 add_AT_int (die, DW_AT_bit_offset, bit_offset);
20172 else
20173 add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
20174}
20175
20176/* For a FIELD_DECL node which represents a bit field, output an attribute
20177 which specifies the length in bits of the given field. */
20178
20179static inline void
20180add_bit_size_attribute (dw_die_ref die, tree decl)
20181{
20182 /* Must be a field and a bit field. */
20183 gcc_assert (TREE_CODE (decl) == FIELD_DECL
20184 && DECL_BIT_FIELD_TYPE (decl));
20185
20186 if (tree_fits_uhwi_p (DECL_SIZE (decl)))
20187 add_AT_unsigned (die, DW_AT_bit_size, tree_to_uhwi (DECL_SIZE (decl)));
20188}
20189
20190/* If the compiled language is ANSI C, then add a 'prototyped'
20191 attribute, if arg types are given for the parameters of a function. */
20192
20193static inline void
20194add_prototyped_attribute (dw_die_ref die, tree func_type)
20195{
20196 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
20197 {
20198 case DW_LANG_C:
20199 case DW_LANG_C89:
20200 case DW_LANG_C99:
20201 case DW_LANG_C11:
20202 case DW_LANG_ObjC:
20203 if (prototype_p (func_type))
20204 add_AT_flag (die, DW_AT_prototyped, 1);
20205 break;
20206 default:
20207 break;
20208 }
20209}
20210
20211/* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
20212 by looking in the type declaration, the object declaration equate table or
20213 the block mapping. */
20214
20215static inline dw_die_ref
20216add_abstract_origin_attribute (dw_die_ref die, tree origin)
20217{
20218 dw_die_ref origin_die = NULL;
20219
20220 if (TREE_CODE (origin) != FUNCTION_DECL
20221 && TREE_CODE (origin) != BLOCK)
20222 {
20223 /* We may have gotten separated from the block for the inlined
20224 function, if we're in an exception handler or some such; make
20225 sure that the abstract function has been written out.
20226
20227 Doing this for nested functions is wrong, however; functions are
20228 distinct units, and our context might not even be inline. */
20229 tree fn = origin;
20230
20231 if (TYPE_P (fn))
20232 fn = TYPE_STUB_DECL (fn);
20233
20234 fn = decl_function_context (fn);
20235 if (fn)
20236 dwarf2out_abstract_function (fn);
20237 }
20238
20239 if (DECL_P (origin))
20240 origin_die = lookup_decl_die (origin);
20241 else if (TYPE_P (origin))
20242 origin_die = lookup_type_die (origin);
20243 else if (TREE_CODE (origin) == BLOCK)
20244 origin_die = BLOCK_DIE (origin);
20245
20246 /* XXX: Functions that are never lowered don't always have correct block
20247 trees (in the case of java, they simply have no block tree, in some other
20248 languages). For these functions, there is nothing we can really do to
20249 output correct debug info for inlined functions in all cases. Rather
20250 than die, we'll just produce deficient debug info now, in that we will
20251 have variables without a proper abstract origin. In the future, when all
20252 functions are lowered, we should re-add a gcc_assert (origin_die)
20253 here. */
20254
20255 if (origin_die)
20256 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
20257 return origin_die;
20258}
20259
20260/* We do not currently support the pure_virtual attribute. */
20261
20262static inline void
20263add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
20264{
20265 if (DECL_VINDEX (func_decl))
20266 {
20267 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
20268
20269 if (tree_fits_shwi_p (DECL_VINDEX (func_decl)))
20270 add_AT_loc (die, DW_AT_vtable_elem_location,
20271 new_loc_descr (DW_OP_constu,
20272 tree_to_shwi (DECL_VINDEX (func_decl)),
20273 0));
20274
20275 /* GNU extension: Record what type this method came from originally. */
20276 if (debug_info_level > DINFO_LEVEL_TERSE
20277 && DECL_CONTEXT (func_decl))
20278 add_AT_die_ref (die, DW_AT_containing_type,
20279 lookup_type_die (DECL_CONTEXT (func_decl)));
20280 }
20281}
20282
20283/* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
20284 given decl. This used to be a vendor extension until after DWARF 4
20285 standardized it. */
20286
20287static void
20288add_linkage_attr (dw_die_ref die, tree decl)
20289{
20290 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
20291
20292 /* Mimic what assemble_name_raw does with a leading '*'. */
20293 if (name[0] == '*')
20294 name = &name[1];
20295
20296 if (dwarf_version >= 4)
20297 add_AT_string (die, DW_AT_linkage_name, name);
20298 else
20299 add_AT_string (die, DW_AT_MIPS_linkage_name, name);
20300}
20301
20302/* Add source coordinate attributes for the given decl. */
20303
20304static void
20305add_src_coords_attributes (dw_die_ref die, tree decl)
20306{
20307 expanded_location s;
20308
20309 if (LOCATION_LOCUS (DECL_SOURCE_LOCATION (decl)) == UNKNOWN_LOCATION)
20310 return;
20311 s = expand_location (DECL_SOURCE_LOCATION (decl));
20312 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
20313 add_AT_unsigned (die, DW_AT_decl_line, s.line);
20314 if (debug_column_info && s.column)
20315 add_AT_unsigned (die, DW_AT_decl_column, s.column);
20316}
20317
20318/* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl. */
20319
20320static void
20321add_linkage_name_raw (dw_die_ref die, tree decl)
20322{
20323 /* Defer until we have an assembler name set. */
20324 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
20325 {
20326 limbo_die_node *asm_name;
20327
20328 asm_name = ggc_cleared_alloc<limbo_die_node> ();
20329 asm_name->die = die;
20330 asm_name->created_for = decl;
20331 asm_name->next = deferred_asm_name;
20332 deferred_asm_name = asm_name;
20333 }
20334 else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
20335 add_linkage_attr (die, decl);
20336}
20337
20338/* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl if desired. */
20339
20340static void
20341add_linkage_name (dw_die_ref die, tree decl)
20342{
20343 if (debug_info_level > DINFO_LEVEL_NONE
20344 && VAR_OR_FUNCTION_DECL_P (decl)
20345 && TREE_PUBLIC (decl)
20346 && !(VAR_P (decl) && DECL_REGISTER (decl))
20347 && die->die_tag != DW_TAG_member)
20348 add_linkage_name_raw (die, decl);
20349}
20350
20351/* Add a DW_AT_name attribute and source coordinate attribute for the
20352 given decl, but only if it actually has a name. */
20353
20354static void
20355add_name_and_src_coords_attributes (dw_die_ref die, tree decl,
20356 bool no_linkage_name)
20357{
20358 tree decl_name;
20359
20360 decl_name = DECL_NAME (decl);
20361 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
20362 {
20363 const char *name = dwarf2_name (decl, 0);
20364 if (name)
20365 add_name_attribute (die, name);
20366 if (! DECL_ARTIFICIAL (decl))
20367 add_src_coords_attributes (die, decl);
20368
20369 if (!no_linkage_name)
20370 add_linkage_name (die, decl);
20371 }
20372
20373#ifdef VMS_DEBUGGING_INFO
20374 /* Get the function's name, as described by its RTL. This may be different
20375 from the DECL_NAME name used in the source file. */
20376 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
20377 {
20378 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
20379 XEXP (DECL_RTL (decl), 0), false);
20380 vec_safe_push (used_rtx_array, XEXP (DECL_RTL (decl), 0));
20381 }
20382#endif /* VMS_DEBUGGING_INFO */
20383}
20384
20385/* Add VALUE as a DW_AT_discr_value attribute to DIE. */
20386
20387static void
20388add_discr_value (dw_die_ref die, dw_discr_value *value)
20389{
20390 dw_attr_node attr;
20391
20392 attr.dw_attr = DW_AT_discr_value;
20393 attr.dw_attr_val.val_class = dw_val_class_discr_value;
20394 attr.dw_attr_val.val_entry = NULL;
20395 attr.dw_attr_val.v.val_discr_value.pos = value->pos;
20396 if (value->pos)
20397 attr.dw_attr_val.v.val_discr_value.v.uval = value->v.uval;
20398 else
20399 attr.dw_attr_val.v.val_discr_value.v.sval = value->v.sval;
20400 add_dwarf_attr (die, &attr);
20401}
20402
20403/* Add DISCR_LIST as a DW_AT_discr_list to DIE. */
20404
20405static void
20406add_discr_list (dw_die_ref die, dw_discr_list_ref discr_list)
20407{
20408 dw_attr_node attr;
20409
20410 attr.dw_attr = DW_AT_discr_list;
20411 attr.dw_attr_val.val_class = dw_val_class_discr_list;
20412 attr.dw_attr_val.val_entry = NULL;
20413 attr.dw_attr_val.v.val_discr_list = discr_list;
20414 add_dwarf_attr (die, &attr);
20415}
20416
20417static inline dw_discr_list_ref
20418AT_discr_list (dw_attr_node *attr)
20419{
20420 return attr->dw_attr_val.v.val_discr_list;
20421}
20422
20423#ifdef VMS_DEBUGGING_INFO
20424/* Output the debug main pointer die for VMS */
20425
20426void
20427dwarf2out_vms_debug_main_pointer (void)
20428{
20429 char label[MAX_ARTIFICIAL_LABEL_BYTES];
20430 dw_die_ref die;
20431
20432 /* Allocate the VMS debug main subprogram die. */
20433 die = ggc_cleared_alloc<die_node> ();
20434 die->die_tag = DW_TAG_subprogram;
20435 add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
20436 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
20437 current_function_funcdef_no);
20438 add_AT_lbl_id (die, DW_AT_entry_pc, label);
20439
20440 /* Make it the first child of comp_unit_die (). */
20441 die->die_parent = comp_unit_die ();
20442 if (comp_unit_die ()->die_child)
20443 {
20444 die->die_sib = comp_unit_die ()->die_child->die_sib;
20445 comp_unit_die ()->die_child->die_sib = die;
20446 }
20447 else
20448 {
20449 die->die_sib = die;
20450 comp_unit_die ()->die_child = die;
20451 }
20452}
20453#endif /* VMS_DEBUGGING_INFO */
20454
20455/* Push a new declaration scope. */
20456
20457static void
20458push_decl_scope (tree scope)
20459{
20460 vec_safe_push (decl_scope_table, scope);
20461}
20462
20463/* Pop a declaration scope. */
20464
20465static inline void
20466pop_decl_scope (void)
20467{
20468 decl_scope_table->pop ();
20469}
20470
20471/* walk_tree helper function for uses_local_type, below. */
20472
20473static tree
20474uses_local_type_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
20475{
20476 if (!TYPE_P (*tp))
20477 *walk_subtrees = 0;
20478 else
20479 {
20480 tree name = TYPE_NAME (*tp);
20481 if (name && DECL_P (name) && decl_function_context (name))
20482 return *tp;
20483 }
20484 return NULL_TREE;
20485}
20486
20487/* If TYPE involves a function-local type (including a local typedef to a
20488 non-local type), returns that type; otherwise returns NULL_TREE. */
20489
20490static tree
20491uses_local_type (tree type)
20492{
20493 tree used = walk_tree_without_duplicates (&type, uses_local_type_r, NULL);
20494 return used;
20495}
20496
20497/* Return the DIE for the scope that immediately contains this type.
20498 Non-named types that do not involve a function-local type get global
20499 scope. Named types nested in namespaces or other types get their
20500 containing scope. All other types (i.e. function-local named types) get
20501 the current active scope. */
20502
20503static dw_die_ref
20504scope_die_for (tree t, dw_die_ref context_die)
20505{
20506 dw_die_ref scope_die = NULL;
20507 tree containing_scope;
20508
20509 /* Non-types always go in the current scope. */
20510 gcc_assert (TYPE_P (t));
20511
20512 /* Use the scope of the typedef, rather than the scope of the type
20513 it refers to. */
20514 if (TYPE_NAME (t) && DECL_P (TYPE_NAME (t)))
20515 containing_scope = DECL_CONTEXT (TYPE_NAME (t));
20516 else
20517 containing_scope = TYPE_CONTEXT (t);
20518
20519 /* Use the containing namespace if there is one. */
20520 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
20521 {
20522 if (context_die == lookup_decl_die (containing_scope))
20523 /* OK */;
20524 else if (debug_info_level > DINFO_LEVEL_TERSE)
20525 context_die = get_context_die (containing_scope);
20526 else
20527 containing_scope = NULL_TREE;
20528 }
20529
20530 /* Ignore function type "scopes" from the C frontend. They mean that
20531 a tagged type is local to a parmlist of a function declarator, but
20532 that isn't useful to DWARF. */
20533 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
20534 containing_scope = NULL_TREE;
20535
20536 if (SCOPE_FILE_SCOPE_P (containing_scope))
20537 {
20538 /* If T uses a local type keep it local as well, to avoid references
20539 to function-local DIEs from outside the function. */
20540 if (current_function_decl && uses_local_type (t))
20541 scope_die = context_die;
20542 else
20543 scope_die = comp_unit_die ();
20544 }
20545 else if (TYPE_P (containing_scope))
20546 {
20547 /* For types, we can just look up the appropriate DIE. */
20548 if (debug_info_level > DINFO_LEVEL_TERSE)
20549 scope_die = get_context_die (containing_scope);
20550 else
20551 {
20552 scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
20553 if (scope_die == NULL)
20554 scope_die = comp_unit_die ();
20555 }
20556 }
20557 else
20558 scope_die = context_die;
20559
20560 return scope_die;
20561}
20562
20563/* Returns nonzero if CONTEXT_DIE is internal to a function. */
20564
20565static inline int
20566local_scope_p (dw_die_ref context_die)
20567{
20568 for (; context_die; context_die = context_die->die_parent)
20569 if (context_die->die_tag == DW_TAG_inlined_subroutine
20570 || context_die->die_tag == DW_TAG_subprogram)
20571 return 1;
20572
20573 return 0;
20574}
20575
20576/* Returns nonzero if CONTEXT_DIE is a class. */
20577
20578static inline int
20579class_scope_p (dw_die_ref context_die)
20580{
20581 return (context_die
20582 && (context_die->die_tag == DW_TAG_structure_type
20583 || context_die->die_tag == DW_TAG_class_type
20584 || context_die->die_tag == DW_TAG_interface_type
20585 || context_die->die_tag == DW_TAG_union_type));
20586}
20587
20588/* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
20589 whether or not to treat a DIE in this context as a declaration. */
20590
20591static inline int
20592class_or_namespace_scope_p (dw_die_ref context_die)
20593{
20594 return (class_scope_p (context_die)
20595 || (context_die && context_die->die_tag == DW_TAG_namespace));
20596}
20597
20598/* Many forms of DIEs require a "type description" attribute. This
20599 routine locates the proper "type descriptor" die for the type given
20600 by 'type' plus any additional qualifiers given by 'cv_quals', and
20601 adds a DW_AT_type attribute below the given die. */
20602
20603static void
20604add_type_attribute (dw_die_ref object_die, tree type, int cv_quals,
20605 bool reverse, dw_die_ref context_die)
20606{
20607 enum tree_code code = TREE_CODE (type);
20608 dw_die_ref type_die = NULL;
20609
20610 /* ??? If this type is an unnamed subrange type of an integral, floating-point
20611 or fixed-point type, use the inner type. This is because we have no
20612 support for unnamed types in base_type_die. This can happen if this is
20613 an Ada subrange type. Correct solution is emit a subrange type die. */
20614 if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
20615 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
20616 type = TREE_TYPE (type), code = TREE_CODE (type);
20617
20618 if (code == ERROR_MARK
20619 /* Handle a special case. For functions whose return type is void, we
20620 generate *no* type attribute. (Note that no object may have type
20621 `void', so this only applies to function return types). */
20622 || code == VOID_TYPE)
20623 return;
20624
20625 type_die = modified_type_die (type,
20626 cv_quals | TYPE_QUALS_NO_ADDR_SPACE (type),
20627 reverse,
20628 context_die);
20629
20630 if (type_die != NULL)
20631 add_AT_die_ref (object_die, DW_AT_type, type_die);
20632}
20633
20634/* Given an object die, add the calling convention attribute for the
20635 function call type. */
20636static void
20637add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
20638{
20639 enum dwarf_calling_convention value = DW_CC_normal;
20640
20641 value = ((enum dwarf_calling_convention)
20642 targetm.dwarf_calling_convention (TREE_TYPE (decl)));
20643
20644 if (is_fortran ()
20645 && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
20646 {
20647 /* DWARF 2 doesn't provide a way to identify a program's source-level
20648 entry point. DW_AT_calling_convention attributes are only meant
20649 to describe functions' calling conventions. However, lacking a
20650 better way to signal the Fortran main program, we used this for
20651 a long time, following existing custom. Now, DWARF 4 has
20652 DW_AT_main_subprogram, which we add below, but some tools still
20653 rely on the old way, which we thus keep. */
20654 value = DW_CC_program;
20655
20656 if (dwarf_version >= 4 || !dwarf_strict)
20657 add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
20658 }
20659
20660 /* Only add the attribute if the backend requests it, and
20661 is not DW_CC_normal. */
20662 if (value && (value != DW_CC_normal))
20663 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
20664}
20665
20666/* Given a tree pointer to a struct, class, union, or enum type node, return
20667 a pointer to the (string) tag name for the given type, or zero if the type
20668 was declared without a tag. */
20669
20670static const char *
20671type_tag (const_tree type)
20672{
20673 const char *name = 0;
20674
20675 if (TYPE_NAME (type) != 0)
20676 {
20677 tree t = 0;
20678
20679 /* Find the IDENTIFIER_NODE for the type name. */
20680 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
20681 && !TYPE_NAMELESS (type))
20682 t = TYPE_NAME (type);
20683
20684 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
20685 a TYPE_DECL node, regardless of whether or not a `typedef' was
20686 involved. */
20687 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
20688 && ! DECL_IGNORED_P (TYPE_NAME (type)))
20689 {
20690 /* We want to be extra verbose. Don't call dwarf_name if
20691 DECL_NAME isn't set. The default hook for decl_printable_name
20692 doesn't like that, and in this context it's correct to return
20693 0, instead of "<anonymous>" or the like. */
20694 if (DECL_NAME (TYPE_NAME (type))
20695 && !DECL_NAMELESS (TYPE_NAME (type)))
20696 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
20697 }
20698
20699 /* Now get the name as a string, or invent one. */
20700 if (!name && t != 0)
20701 name = IDENTIFIER_POINTER (t);
20702 }
20703
20704 return (name == 0 || *name == '\0') ? 0 : name;
20705}
20706
20707/* Return the type associated with a data member, make a special check
20708 for bit field types. */
20709
20710static inline tree
20711member_declared_type (const_tree member)
20712{
20713 return (DECL_BIT_FIELD_TYPE (member)
20714 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
20715}
20716
20717/* Get the decl's label, as described by its RTL. This may be different
20718 from the DECL_NAME name used in the source file. */
20719
20720#if 0
20721static const char *
20722decl_start_label (tree decl)
20723{
20724 rtx x;
20725 const char *fnname;
20726
20727 x = DECL_RTL (decl);
20728 gcc_assert (MEM_P (x));
20729
20730 x = XEXP (x, 0);
20731 gcc_assert (GET_CODE (x) == SYMBOL_REF);
20732
20733 fnname = XSTR (x, 0);
20734 return fnname;
20735}
20736#endif
20737
20738/* For variable-length arrays that have been previously generated, but
20739 may be incomplete due to missing subscript info, fill the subscript
20740 info. Return TRUE if this is one of those cases. */
20741static bool
20742fill_variable_array_bounds (tree type)
20743{
20744 if (TREE_ASM_WRITTEN (type)
20745 && TREE_CODE (type) == ARRAY_TYPE
20746 && variably_modified_type_p (type, NULL))
20747 {
20748 dw_die_ref array_die = lookup_type_die (type);
20749 if (!array_die)
20750 return false;
20751 add_subscript_info (array_die, type, !is_ada ());
20752 return true;
20753 }
20754 return false;
20755}
20756
20757/* These routines generate the internal representation of the DIE's for
20758 the compilation unit. Debugging information is collected by walking
20759 the declaration trees passed in from dwarf2out_decl(). */
20760
20761static void
20762gen_array_type_die (tree type, dw_die_ref context_die)
20763{
20764 dw_die_ref array_die;
20765
20766 /* GNU compilers represent multidimensional array types as sequences of one
20767 dimensional array types whose element types are themselves array types.
20768 We sometimes squish that down to a single array_type DIE with multiple
20769 subscripts in the Dwarf debugging info. The draft Dwarf specification
20770 say that we are allowed to do this kind of compression in C, because
20771 there is no difference between an array of arrays and a multidimensional
20772 array. We don't do this for Ada to remain as close as possible to the
20773 actual representation, which is especially important against the language
20774 flexibilty wrt arrays of variable size. */
20775
20776 bool collapse_nested_arrays = !is_ada ();
20777
20778 if (fill_variable_array_bounds (type))
20779 return;
20780
20781 dw_die_ref scope_die = scope_die_for (type, context_die);
20782 tree element_type;
20783
20784 /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
20785 DW_TAG_string_type doesn't have DW_AT_type attribute). */
20786 if (TYPE_STRING_FLAG (type)
20787 && TREE_CODE (type) == ARRAY_TYPE
20788 && is_fortran ()
20789 && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
20790 {
20791 HOST_WIDE_INT size;
20792
20793 array_die = new_die (DW_TAG_string_type, scope_die, type);
20794 add_name_attribute (array_die, type_tag (type));
20795 equate_type_number_to_die (type, array_die);
20796 size = int_size_in_bytes (type);
20797 if (size >= 0)
20798 add_AT_unsigned (array_die, DW_AT_byte_size, size);
20799 else if (TYPE_DOMAIN (type) != NULL_TREE
20800 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE)
20801 {
20802 tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
20803 tree rszdecl = szdecl;
20804
20805 size = int_size_in_bytes (TREE_TYPE (szdecl));
20806 if (!DECL_P (szdecl))
20807 {
20808 if (TREE_CODE (szdecl) == INDIRECT_REF
20809 && DECL_P (TREE_OPERAND (szdecl, 0)))
20810 {
20811 rszdecl = TREE_OPERAND (szdecl, 0);
20812 if (int_size_in_bytes (TREE_TYPE (rszdecl))
20813 != DWARF2_ADDR_SIZE)
20814 size = 0;
20815 }
20816 else
20817 size = 0;
20818 }
20819 if (size > 0)
20820 {
20821 dw_loc_list_ref loc
20822 = loc_list_from_tree (rszdecl, szdecl == rszdecl ? 2 : 0,
20823 NULL);
20824 if (loc)
20825 {
20826 add_AT_location_description (array_die, DW_AT_string_length,
20827 loc);
20828 if (size != DWARF2_ADDR_SIZE)
20829 add_AT_unsigned (array_die, dwarf_version >= 5
20830 ? DW_AT_string_length_byte_size
20831 : DW_AT_byte_size, size);
20832 }
20833 }
20834 }
20835 return;
20836 }
20837
20838 array_die = new_die (DW_TAG_array_type, scope_die, type);
20839 add_name_attribute (array_die, type_tag (type));
20840 equate_type_number_to_die (type, array_die);
20841
20842 if (TREE_CODE (type) == VECTOR_TYPE)
20843 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
20844
20845 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
20846 if (is_fortran ()
20847 && TREE_CODE (type) == ARRAY_TYPE
20848 && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
20849 && !TYPE_STRING_FLAG (TREE_TYPE (type)))
20850 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
20851
20852#if 0
20853 /* We default the array ordering. SDB will probably do
20854 the right things even if DW_AT_ordering is not present. It's not even
20855 an issue until we start to get into multidimensional arrays anyway. If
20856 SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
20857 then we'll have to put the DW_AT_ordering attribute back in. (But if
20858 and when we find out that we need to put these in, we will only do so
20859 for multidimensional arrays. */
20860 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
20861#endif
20862
20863 if (TREE_CODE (type) == VECTOR_TYPE)
20864 {
20865 /* For VECTOR_TYPEs we use an array die with appropriate bounds. */
20866 dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
20867 add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node, NULL);
20868 add_bound_info (subrange_die, DW_AT_upper_bound,
20869 size_int (TYPE_VECTOR_SUBPARTS (type) - 1), NULL);
20870 }
20871 else
20872 add_subscript_info (array_die, type, collapse_nested_arrays);
20873
20874 /* Add representation of the type of the elements of this array type and
20875 emit the corresponding DIE if we haven't done it already. */
20876 element_type = TREE_TYPE (type);
20877 if (collapse_nested_arrays)
20878 while (TREE_CODE (element_type) == ARRAY_TYPE)
20879 {
20880 if (TYPE_STRING_FLAG (element_type) && is_fortran ())
20881 break;
20882 element_type = TREE_TYPE (element_type);
20883 }
20884
20885 add_type_attribute (array_die, element_type, TYPE_UNQUALIFIED,
20886 TREE_CODE (type) == ARRAY_TYPE
20887 && TYPE_REVERSE_STORAGE_ORDER (type),
20888 context_die);
20889
20890 add_gnat_descriptive_type_attribute (array_die, type, context_die);
20891 if (TYPE_ARTIFICIAL (type))
20892 add_AT_flag (array_die, DW_AT_artificial, 1);
20893
20894 if (get_AT (array_die, DW_AT_name))
20895 add_pubtype (type, array_die);
20896
20897 add_alignment_attribute (array_die, type);
20898}
20899
20900/* This routine generates DIE for array with hidden descriptor, details
20901 are filled into *info by a langhook. */
20902
20903static void
20904gen_descr_array_type_die (tree type, struct array_descr_info *info,
20905 dw_die_ref context_die)
20906{
20907 const dw_die_ref scope_die = scope_die_for (type, context_die);
20908 const dw_die_ref array_die = new_die (DW_TAG_array_type, scope_die, type);
20909 struct loc_descr_context context = { type, info->base_decl, NULL,
20910 false, false };
20911 enum dwarf_tag subrange_tag = DW_TAG_subrange_type;
20912 int dim;
20913
20914 add_name_attribute (array_die, type_tag (type));
20915 equate_type_number_to_die (type, array_die);
20916
20917 if (info->ndimensions > 1)
20918 switch (info->ordering)
20919 {
20920 case array_descr_ordering_row_major:
20921 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
20922 break;
20923 case array_descr_ordering_column_major:
20924 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
20925 break;
20926 default:
20927 break;
20928 }
20929
20930 if (dwarf_version >= 3 || !dwarf_strict)
20931 {
20932 if (info->data_location)
20933 add_scalar_info (array_die, DW_AT_data_location, info->data_location,
20934 dw_scalar_form_exprloc, &context);
20935 if (info->associated)
20936 add_scalar_info (array_die, DW_AT_associated, info->associated,
20937 dw_scalar_form_constant
20938 | dw_scalar_form_exprloc
20939 | dw_scalar_form_reference, &context);
20940 if (info->allocated)
20941 add_scalar_info (array_die, DW_AT_allocated, info->allocated,
20942 dw_scalar_form_constant
20943 | dw_scalar_form_exprloc
20944 | dw_scalar_form_reference, &context);
20945 if (info->stride)
20946 {
20947 const enum dwarf_attribute attr
20948 = (info->stride_in_bits) ? DW_AT_bit_stride : DW_AT_byte_stride;
20949 const int forms
20950 = (info->stride_in_bits)
20951 ? dw_scalar_form_constant
20952 : (dw_scalar_form_constant
20953 | dw_scalar_form_exprloc
20954 | dw_scalar_form_reference);
20955
20956 add_scalar_info (array_die, attr, info->stride, forms, &context);
20957 }
20958 }
20959 if (dwarf_version >= 5)
20960 {
20961 if (info->rank)
20962 {
20963 add_scalar_info (array_die, DW_AT_rank, info->rank,
20964 dw_scalar_form_constant
20965 | dw_scalar_form_exprloc, &context);
20966 subrange_tag = DW_TAG_generic_subrange;
20967 context.placeholder_arg = true;
20968 }
20969 }
20970
20971 add_gnat_descriptive_type_attribute (array_die, type, context_die);
20972
20973 for (dim = 0; dim < info->ndimensions; dim++)
20974 {
20975 dw_die_ref subrange_die = new_die (subrange_tag, array_die, NULL);
20976
20977 if (info->dimen[dim].bounds_type)
20978 add_type_attribute (subrange_die,
20979 info->dimen[dim].bounds_type, TYPE_UNQUALIFIED,
20980 false, context_die);
20981 if (info->dimen[dim].lower_bound)
20982 add_bound_info (subrange_die, DW_AT_lower_bound,
20983 info->dimen[dim].lower_bound, &context);
20984 if (info->dimen[dim].upper_bound)
20985 add_bound_info (subrange_die, DW_AT_upper_bound,
20986 info->dimen[dim].upper_bound, &context);
20987 if ((dwarf_version >= 3 || !dwarf_strict) && info->dimen[dim].stride)
20988 add_scalar_info (subrange_die, DW_AT_byte_stride,
20989 info->dimen[dim].stride,
20990 dw_scalar_form_constant
20991 | dw_scalar_form_exprloc
20992 | dw_scalar_form_reference,
20993 &context);
20994 }
20995
20996 gen_type_die (info->element_type, context_die);
20997 add_type_attribute (array_die, info->element_type, TYPE_UNQUALIFIED,
20998 TREE_CODE (type) == ARRAY_TYPE
20999 && TYPE_REVERSE_STORAGE_ORDER (type),
21000 context_die);
21001
21002 if (get_AT (array_die, DW_AT_name))
21003 add_pubtype (type, array_die);
21004
21005 add_alignment_attribute (array_die, type);
21006}
21007
21008#if 0
21009static void
21010gen_entry_point_die (tree decl, dw_die_ref context_die)
21011{
21012 tree origin = decl_ultimate_origin (decl);
21013 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
21014
21015 if (origin != NULL)
21016 add_abstract_origin_attribute (decl_die, origin);
21017 else
21018 {
21019 add_name_and_src_coords_attributes (decl_die, decl);
21020 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
21021 TYPE_UNQUALIFIED, false, context_die);
21022 }
21023
21024 if (DECL_ABSTRACT_P (decl))
21025 equate_decl_number_to_die (decl, decl_die);
21026 else
21027 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
21028}
21029#endif
21030
21031/* Walk through the list of incomplete types again, trying once more to
21032 emit full debugging info for them. */
21033
21034static void
21035retry_incomplete_types (void)
21036{
21037 set_early_dwarf s;
21038 int i;
21039
21040 for (i = vec_safe_length (incomplete_types) - 1; i >= 0; i--)
21041 if (should_emit_struct_debug ((*incomplete_types)[i], DINFO_USAGE_DIR_USE))
21042 gen_type_die ((*incomplete_types)[i], comp_unit_die ());
21043 vec_safe_truncate (incomplete_types, 0);
21044}
21045
21046/* Determine what tag to use for a record type. */
21047
21048static enum dwarf_tag
21049record_type_tag (tree type)
21050{
21051 if (! lang_hooks.types.classify_record)
21052 return DW_TAG_structure_type;
21053
21054 switch (lang_hooks.types.classify_record (type))
21055 {
21056 case RECORD_IS_STRUCT:
21057 return DW_TAG_structure_type;
21058
21059 case RECORD_IS_CLASS:
21060 return DW_TAG_class_type;
21061
21062 case RECORD_IS_INTERFACE:
21063 if (dwarf_version >= 3 || !dwarf_strict)
21064 return DW_TAG_interface_type;
21065 return DW_TAG_structure_type;
21066
21067 default:
21068 gcc_unreachable ();
21069 }
21070}
21071
21072/* Generate a DIE to represent an enumeration type. Note that these DIEs
21073 include all of the information about the enumeration values also. Each
21074 enumerated type name/value is listed as a child of the enumerated type
21075 DIE. */
21076
21077static dw_die_ref
21078gen_enumeration_type_die (tree type, dw_die_ref context_die)
21079{
21080 dw_die_ref type_die = lookup_type_die (type);
21081
21082 if (type_die == NULL)
21083 {
21084 type_die = new_die (DW_TAG_enumeration_type,
21085 scope_die_for (type, context_die), type);
21086 equate_type_number_to_die (type, type_die);
21087 add_name_attribute (type_die, type_tag (type));
21088 if (dwarf_version >= 4 || !dwarf_strict)
21089 {
21090 if (ENUM_IS_SCOPED (type))
21091 add_AT_flag (type_die, DW_AT_enum_class, 1);
21092 if (ENUM_IS_OPAQUE (type))
21093 add_AT_flag (type_die, DW_AT_declaration, 1);
21094 }
21095 if (!dwarf_strict)
21096 add_AT_unsigned (type_die, DW_AT_encoding,
21097 TYPE_UNSIGNED (type)
21098 ? DW_ATE_unsigned
21099 : DW_ATE_signed);
21100 }
21101 else if (! TYPE_SIZE (type))
21102 return type_die;
21103 else
21104 remove_AT (type_die, DW_AT_declaration);
21105
21106 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
21107 given enum type is incomplete, do not generate the DW_AT_byte_size
21108 attribute or the DW_AT_element_list attribute. */
21109 if (TYPE_SIZE (type))
21110 {
21111 tree link;
21112
21113 TREE_ASM_WRITTEN (type) = 1;
21114 add_byte_size_attribute (type_die, type);
21115 add_alignment_attribute (type_die, type);
21116 if (dwarf_version >= 3 || !dwarf_strict)
21117 {
21118 tree underlying = lang_hooks.types.enum_underlying_base_type (type);
21119 add_type_attribute (type_die, underlying, TYPE_UNQUALIFIED, false,
21120 context_die);
21121 }
21122 if (TYPE_STUB_DECL (type) != NULL_TREE)
21123 {
21124 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
21125 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
21126 }
21127
21128 /* If the first reference to this type was as the return type of an
21129 inline function, then it may not have a parent. Fix this now. */
21130 if (type_die->die_parent == NULL)
21131 add_child_die (scope_die_for (type, context_die), type_die);
21132
21133 for (link = TYPE_VALUES (type);
21134 link != NULL; link = TREE_CHAIN (link))
21135 {
21136 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
21137 tree value = TREE_VALUE (link);
21138
21139 add_name_attribute (enum_die,
21140 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
21141
21142 if (TREE_CODE (value) == CONST_DECL)
21143 value = DECL_INITIAL (value);
21144
21145 if (simple_type_size_in_bits (TREE_TYPE (value))
21146 <= HOST_BITS_PER_WIDE_INT || tree_fits_shwi_p (value))
21147 {
21148 /* For constant forms created by add_AT_unsigned DWARF
21149 consumers (GDB, elfutils, etc.) always zero extend
21150 the value. Only when the actual value is negative
21151 do we need to use add_AT_int to generate a constant
21152 form that can represent negative values. */
21153 HOST_WIDE_INT val = TREE_INT_CST_LOW (value);
21154 if (TYPE_UNSIGNED (TREE_TYPE (value)) || val >= 0)
21155 add_AT_unsigned (enum_die, DW_AT_const_value,
21156 (unsigned HOST_WIDE_INT) val);
21157 else
21158 add_AT_int (enum_die, DW_AT_const_value, val);
21159 }
21160 else
21161 /* Enumeration constants may be wider than HOST_WIDE_INT. Handle
21162 that here. TODO: This should be re-worked to use correct
21163 signed/unsigned double tags for all cases. */
21164 add_AT_wide (enum_die, DW_AT_const_value, value);
21165 }
21166
21167 add_gnat_descriptive_type_attribute (type_die, type, context_die);
21168 if (TYPE_ARTIFICIAL (type))
21169 add_AT_flag (type_die, DW_AT_artificial, 1);
21170 }
21171 else
21172 add_AT_flag (type_die, DW_AT_declaration, 1);
21173
21174 add_alignment_attribute (type_die, type);
21175
21176 add_pubtype (type, type_die);
21177
21178 return type_die;
21179}
21180
21181/* Generate a DIE to represent either a real live formal parameter decl or to
21182 represent just the type of some formal parameter position in some function
21183 type.
21184
21185 Note that this routine is a bit unusual because its argument may be a
21186 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
21187 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
21188 node. If it's the former then this function is being called to output a
21189 DIE to represent a formal parameter object (or some inlining thereof). If
21190 it's the latter, then this function is only being called to output a
21191 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
21192 argument type of some subprogram type.
21193 If EMIT_NAME_P is true, name and source coordinate attributes
21194 are emitted. */
21195
21196static dw_die_ref
21197gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
21198 dw_die_ref context_die)
21199{
21200 tree node_or_origin = node ? node : origin;
21201 tree ultimate_origin;
21202 dw_die_ref parm_die = NULL;
21203
21204 if (TREE_CODE_CLASS (TREE_CODE (node_or_origin)) == tcc_declaration)
21205 {
21206 parm_die = lookup_decl_die (node);
21207
21208 /* If the contexts differ, we may not be talking about the same
21209 thing. */
21210 if (parm_die && parm_die->die_parent != context_die)
21211 {
21212 if (!DECL_ABSTRACT_P (node))
21213 {
21214 /* This can happen when creating an inlined instance, in
21215 which case we need to create a new DIE that will get
21216 annotated with DW_AT_abstract_origin. */
21217 parm_die = NULL;
21218 }
21219 else
21220 {
21221 /* FIXME: Reuse DIE even with a differing context.
21222
21223 This can happen when calling
21224 dwarf2out_abstract_function to build debug info for
21225 the abstract instance of a function for which we have
21226 already generated a DIE in
21227 dwarf2out_early_global_decl.
21228
21229 Once we remove dwarf2out_abstract_function, we should
21230 have a call to gcc_unreachable here. */
21231 }
21232 }
21233
21234 if (parm_die && parm_die->die_parent == NULL)
21235 {
21236 /* Check that parm_die already has the right attributes that
21237 we would have added below. If any attributes are
21238 missing, fall through to add them. */
21239 if (! DECL_ABSTRACT_P (node_or_origin)
21240 && !get_AT (parm_die, DW_AT_location)
21241 && !get_AT (parm_die, DW_AT_const_value))
21242 /* We are missing location info, and are about to add it. */
21243 ;
21244 else
21245 {
21246 add_child_die (context_die, parm_die);
21247 return parm_die;
21248 }
21249 }
21250 }
21251
21252 /* If we have a previously generated DIE, use it, unless this is an
21253 concrete instance (origin != NULL), in which case we need a new
21254 DIE with a corresponding DW_AT_abstract_origin. */
21255 bool reusing_die;
21256 if (parm_die && origin == NULL)
21257 reusing_die = true;
21258 else
21259 {
21260 parm_die = new_die (DW_TAG_formal_parameter, context_die, node);
21261 reusing_die = false;
21262 }
21263
21264 switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
21265 {
21266 case tcc_declaration:
21267 ultimate_origin = decl_ultimate_origin (node_or_origin);
21268 if (node || ultimate_origin)
21269 origin = ultimate_origin;
21270
21271 if (reusing_die)
21272 goto add_location;
21273
21274 if (origin != NULL)
21275 add_abstract_origin_attribute (parm_die, origin);
21276 else if (emit_name_p)
21277 add_name_and_src_coords_attributes (parm_die, node);
21278 if (origin == NULL
21279 || (! DECL_ABSTRACT_P (node_or_origin)
21280 && variably_modified_type_p (TREE_TYPE (node_or_origin),
21281 decl_function_context
21282 (node_or_origin))))
21283 {
21284 tree type = TREE_TYPE (node_or_origin);
21285 if (decl_by_reference_p (node_or_origin))
21286 add_type_attribute (parm_die, TREE_TYPE (type),
21287 TYPE_UNQUALIFIED,
21288 false, context_die);
21289 else
21290 add_type_attribute (parm_die, type,
21291 decl_quals (node_or_origin),
21292 false, context_die);
21293 }
21294 if (origin == NULL && DECL_ARTIFICIAL (node))
21295 add_AT_flag (parm_die, DW_AT_artificial, 1);
21296 add_location:
21297 if (node && node != origin)
21298 equate_decl_number_to_die (node, parm_die);
21299 if (! DECL_ABSTRACT_P (node_or_origin))
21300 add_location_or_const_value_attribute (parm_die, node_or_origin,
21301 node == NULL);
21302
21303 break;
21304
21305 case tcc_type:
21306 /* We were called with some kind of a ..._TYPE node. */
21307 add_type_attribute (parm_die, node_or_origin, TYPE_UNQUALIFIED, false,
21308 context_die);
21309 break;
21310
21311 default:
21312 gcc_unreachable ();
21313 }
21314
21315 return parm_die;
21316}
21317
21318/* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
21319 children DW_TAG_formal_parameter DIEs representing the arguments of the
21320 parameter pack.
21321
21322 PARM_PACK must be a function parameter pack.
21323 PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
21324 must point to the subsequent arguments of the function PACK_ARG belongs to.
21325 SUBR_DIE is the DIE of the function PACK_ARG belongs to.
21326 If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
21327 following the last one for which a DIE was generated. */
21328
21329static dw_die_ref
21330gen_formal_parameter_pack_die (tree parm_pack,
21331 tree pack_arg,
21332 dw_die_ref subr_die,
21333 tree *next_arg)
21334{
21335 tree arg;
21336 dw_die_ref parm_pack_die;
21337
21338 gcc_assert (parm_pack
21339 && lang_hooks.function_parameter_pack_p (parm_pack)
21340 && subr_die);
21341
21342 parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
21343 add_src_coords_attributes (parm_pack_die, parm_pack);
21344
21345 for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
21346 {
21347 if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
21348 parm_pack))
21349 break;
21350 gen_formal_parameter_die (arg, NULL,
21351 false /* Don't emit name attribute. */,
21352 parm_pack_die);
21353 }
21354 if (next_arg)
21355 *next_arg = arg;
21356 return parm_pack_die;
21357}
21358
21359/* Generate a special type of DIE used as a stand-in for a trailing ellipsis
21360 at the end of an (ANSI prototyped) formal parameters list. */
21361
21362static void
21363gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
21364{
21365 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
21366}
21367
21368/* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
21369 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
21370 parameters as specified in some function type specification (except for
21371 those which appear as part of a function *definition*). */
21372
21373static void
21374gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
21375{
21376 tree link;
21377 tree formal_type = NULL;
21378 tree first_parm_type;
21379 tree arg;
21380
21381 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
21382 {
21383 arg = DECL_ARGUMENTS (function_or_method_type);
21384 function_or_method_type = TREE_TYPE (function_or_method_type);
21385 }
21386 else
21387 arg = NULL_TREE;
21388
21389 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
21390
21391 /* Make our first pass over the list of formal parameter types and output a
21392 DW_TAG_formal_parameter DIE for each one. */
21393 for (link = first_parm_type; link; )
21394 {
21395 dw_die_ref parm_die;
21396
21397 formal_type = TREE_VALUE (link);
21398 if (formal_type == void_type_node)
21399 break;
21400
21401 /* Output a (nameless) DIE to represent the formal parameter itself. */
21402 if (!POINTER_BOUNDS_TYPE_P (formal_type))
21403 {
21404 parm_die = gen_formal_parameter_die (formal_type, NULL,
21405 true /* Emit name attribute. */,
21406 context_die);
21407 if (TREE_CODE (function_or_method_type) == METHOD_TYPE
21408 && link == first_parm_type)
21409 {
21410 add_AT_flag (parm_die, DW_AT_artificial, 1);
21411 if (dwarf_version >= 3 || !dwarf_strict)
21412 add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
21413 }
21414 else if (arg && DECL_ARTIFICIAL (arg))
21415 add_AT_flag (parm_die, DW_AT_artificial, 1);
21416 }
21417
21418 link = TREE_CHAIN (link);
21419 if (arg)
21420 arg = DECL_CHAIN (arg);
21421 }
21422
21423 /* If this function type has an ellipsis, add a
21424 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
21425 if (formal_type != void_type_node)
21426 gen_unspecified_parameters_die (function_or_method_type, context_die);
21427
21428 /* Make our second (and final) pass over the list of formal parameter types
21429 and output DIEs to represent those types (as necessary). */
21430 for (link = TYPE_ARG_TYPES (function_or_method_type);
21431 link && TREE_VALUE (link);
21432 link = TREE_CHAIN (link))
21433 gen_type_die (TREE_VALUE (link), context_die);
21434}
21435
21436/* We want to generate the DIE for TYPE so that we can generate the
21437 die for MEMBER, which has been defined; we will need to refer back
21438 to the member declaration nested within TYPE. If we're trying to
21439 generate minimal debug info for TYPE, processing TYPE won't do the
21440 trick; we need to attach the member declaration by hand. */
21441
21442static void
21443gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
21444{
21445 gen_type_die (type, context_die);
21446
21447 /* If we're trying to avoid duplicate debug info, we may not have
21448 emitted the member decl for this function. Emit it now. */
21449 if (TYPE_STUB_DECL (type)
21450 && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
21451 && ! lookup_decl_die (member))
21452 {
21453 dw_die_ref type_die;
21454 gcc_assert (!decl_ultimate_origin (member));
21455
21456 push_decl_scope (type);
21457 type_die = lookup_type_die_strip_naming_typedef (type);
21458 if (TREE_CODE (member) == FUNCTION_DECL)
21459 gen_subprogram_die (member, type_die);
21460 else if (TREE_CODE (member) == FIELD_DECL)
21461 {
21462 /* Ignore the nameless fields that are used to skip bits but handle
21463 C++ anonymous unions and structs. */
21464 if (DECL_NAME (member) != NULL_TREE
21465 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
21466 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
21467 {
21468 struct vlr_context vlr_ctx = {
21469 DECL_CONTEXT (member), /* struct_type */
21470 NULL_TREE /* variant_part_offset */
21471 };
21472 gen_type_die (member_declared_type (member), type_die);
21473 gen_field_die (member, &vlr_ctx, type_die);
21474 }
21475 }
21476 else
21477 gen_variable_die (member, NULL_TREE, type_die);
21478
21479 pop_decl_scope ();
21480 }
21481}
21482
21483/* Forward declare these functions, because they are mutually recursive
21484 with their set_block_* pairing functions. */
21485static void set_decl_origin_self (tree);
21486static void set_decl_abstract_flags (tree, vec<tree> &);
21487
21488/* Given a pointer to some BLOCK node, if the BLOCK_ABSTRACT_ORIGIN for the
21489 given BLOCK node is NULL, set the BLOCK_ABSTRACT_ORIGIN for the node so
21490 that it points to the node itself, thus indicating that the node is its
21491 own (abstract) origin. Additionally, if the BLOCK_ABSTRACT_ORIGIN for
21492 the given node is NULL, recursively descend the decl/block tree which
21493 it is the root of, and for each other ..._DECL or BLOCK node contained
21494 therein whose DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also
21495 still NULL, set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN
21496 values to point to themselves. */
21497
21498static void
21499set_block_origin_self (tree stmt)
21500{
21501 if (BLOCK_ABSTRACT_ORIGIN (stmt) == NULL_TREE)
21502 {
21503 BLOCK_ABSTRACT_ORIGIN (stmt) = stmt;
21504
21505 {
21506 tree local_decl;
21507
21508 for (local_decl = BLOCK_VARS (stmt);
21509 local_decl != NULL_TREE;
21510 local_decl = DECL_CHAIN (local_decl))
21511 /* Do not recurse on nested functions since the inlining status
21512 of parent and child can be different as per the DWARF spec. */
21513 if (TREE_CODE (local_decl) != FUNCTION_DECL
21514 && !DECL_EXTERNAL (local_decl))
21515 set_decl_origin_self (local_decl);
21516 }
21517
21518 {
21519 tree subblock;
21520
21521 for (subblock = BLOCK_SUBBLOCKS (stmt);
21522 subblock != NULL_TREE;
21523 subblock = BLOCK_CHAIN (subblock))
21524 set_block_origin_self (subblock); /* Recurse. */
21525 }
21526 }
21527}
21528
21529/* Given a pointer to some ..._DECL node, if the DECL_ABSTRACT_ORIGIN for
21530 the given ..._DECL node is NULL, set the DECL_ABSTRACT_ORIGIN for the
21531 node to so that it points to the node itself, thus indicating that the
21532 node represents its own (abstract) origin. Additionally, if the
21533 DECL_ABSTRACT_ORIGIN for the given node is NULL, recursively descend
21534 the decl/block tree of which the given node is the root of, and for
21535 each other ..._DECL or BLOCK node contained therein whose
21536 DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also still NULL,
21537 set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN values to
21538 point to themselves. */
21539
21540static void
21541set_decl_origin_self (tree decl)
21542{
21543 if (DECL_ABSTRACT_ORIGIN (decl) == NULL_TREE)
21544 {
21545 DECL_ABSTRACT_ORIGIN (decl) = decl;
21546 if (TREE_CODE (decl) == FUNCTION_DECL)
21547 {
21548 tree arg;
21549
21550 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
21551 DECL_ABSTRACT_ORIGIN (arg) = arg;
21552 if (DECL_INITIAL (decl) != NULL_TREE
21553 && DECL_INITIAL (decl) != error_mark_node)
21554 set_block_origin_self (DECL_INITIAL (decl));
21555 }
21556 }
21557}
21558
21559/* Given a pointer to some BLOCK node, set the BLOCK_ABSTRACT flag to 1
21560 and if it wasn't 1 before, push it to abstract_vec vector.
21561 For all local decls and all local sub-blocks (recursively) do it
21562 too. */
21563
21564static void
21565set_block_abstract_flags (tree stmt, vec<tree> &abstract_vec)
21566{
21567 tree local_decl;
21568 tree subblock;
21569 unsigned int i;
21570
21571 if (!BLOCK_ABSTRACT (stmt))
21572 {
21573 abstract_vec.safe_push (stmt);
21574 BLOCK_ABSTRACT (stmt) = 1;
21575 }
21576
21577 for (local_decl = BLOCK_VARS (stmt);
21578 local_decl != NULL_TREE;
21579 local_decl = DECL_CHAIN (local_decl))
21580 if (! DECL_EXTERNAL (local_decl))
21581 set_decl_abstract_flags (local_decl, abstract_vec);
21582
21583 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
21584 {
21585 local_decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
21586 if ((VAR_P (local_decl) && !TREE_STATIC (local_decl))
21587 || TREE_CODE (local_decl) == PARM_DECL)
21588 set_decl_abstract_flags (local_decl, abstract_vec);
21589 }
21590
21591 for (subblock = BLOCK_SUBBLOCKS (stmt);
21592 subblock != NULL_TREE;
21593 subblock = BLOCK_CHAIN (subblock))
21594 set_block_abstract_flags (subblock, abstract_vec);
21595}
21596
21597/* Given a pointer to some ..._DECL node, set DECL_ABSTRACT_P flag on it
21598 to 1 and if it wasn't 1 before, push to abstract_vec vector.
21599 In the case where the decl is a FUNCTION_DECL also set the abstract
21600 flags for all of the parameters, local vars, local
21601 blocks and sub-blocks (recursively). */
21602
21603static void
21604set_decl_abstract_flags (tree decl, vec<tree> &abstract_vec)
21605{
21606 if (!DECL_ABSTRACT_P (decl))
21607 {
21608 abstract_vec.safe_push (decl);
21609 DECL_ABSTRACT_P (decl) = 1;
21610 }
21611
21612 if (TREE_CODE (decl) == FUNCTION_DECL)
21613 {
21614 tree arg;
21615
21616 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
21617 if (!DECL_ABSTRACT_P (arg))
21618 {
21619 abstract_vec.safe_push (arg);
21620 DECL_ABSTRACT_P (arg) = 1;
21621 }
21622 if (DECL_INITIAL (decl) != NULL_TREE
21623 && DECL_INITIAL (decl) != error_mark_node)
21624 set_block_abstract_flags (DECL_INITIAL (decl), abstract_vec);
21625 }
21626}
21627
21628/* Generate the DWARF2 info for the "abstract" instance of a function which we
21629 may later generate inlined and/or out-of-line instances of.
21630
21631 FIXME: In the early-dwarf world, this function, and most of the
21632 DECL_ABSTRACT code should be obsoleted. The early DIE _is_
21633 the abstract instance. All we would need to do is annotate
21634 the early DIE with the appropriate DW_AT_inline in late
21635 dwarf (perhaps in gen_inlined_subroutine_die).
21636
21637 However, we can't do this yet, because LTO streaming of DIEs
21638 has not been implemented yet. */
21639
21640static void
21641dwarf2out_abstract_function (tree decl)
21642{
21643 dw_die_ref old_die;
21644 tree save_fn;
21645 tree context;
21646 hash_table<decl_loc_hasher> *old_decl_loc_table;
21647 hash_table<dw_loc_list_hasher> *old_cached_dw_loc_list_table;
21648 int old_call_site_count, old_tail_call_site_count;
21649 struct call_arg_loc_node *old_call_arg_locations;
21650
21651 /* Make sure we have the actual abstract inline, not a clone. */
21652 decl = DECL_ORIGIN (decl);
21653
21654 old_die = lookup_decl_die (decl);
21655 if (old_die && get_AT (old_die, DW_AT_inline))
21656 /* We've already generated the abstract instance. */
21657 return;
21658
21659 /* We can be called while recursively when seeing block defining inlined subroutine
21660 DIE. Be sure to not clobber the outer location table nor use it or we would
21661 get locations in abstract instantces. */
21662 old_decl_loc_table = decl_loc_table;
21663 decl_loc_table = NULL;
21664 old_cached_dw_loc_list_table = cached_dw_loc_list_table;
21665 cached_dw_loc_list_table = NULL;
21666 old_call_arg_locations = call_arg_locations;
21667 call_arg_locations = NULL;
21668 old_call_site_count = call_site_count;
21669 call_site_count = -1;
21670 old_tail_call_site_count = tail_call_site_count;
21671 tail_call_site_count = -1;
21672
21673 /* Be sure we've emitted the in-class declaration DIE (if any) first, so
21674 we don't get confused by DECL_ABSTRACT_P. */
21675 if (debug_info_level > DINFO_LEVEL_TERSE)
21676 {
21677 context = decl_class_context (decl);
21678 if (context)
21679 gen_type_die_for_member
21680 (context, decl, decl_function_context (decl) ? NULL : comp_unit_die ());
21681 }
21682
21683 /* Pretend we've just finished compiling this function. */
21684 save_fn = current_function_decl;
21685 current_function_decl = decl;
21686
21687 auto_vec<tree, 64> abstract_vec;
21688 set_decl_abstract_flags (decl, abstract_vec);
21689 dwarf2out_decl (decl);
21690 unsigned int i;
21691 tree t;
21692 FOR_EACH_VEC_ELT (abstract_vec, i, t)
21693 if (TREE_CODE (t) == BLOCK)
21694 BLOCK_ABSTRACT (t) = 0;
21695 else
21696 DECL_ABSTRACT_P (t) = 0;
21697
21698 current_function_decl = save_fn;
21699 decl_loc_table = old_decl_loc_table;
21700 cached_dw_loc_list_table = old_cached_dw_loc_list_table;
21701 call_arg_locations = old_call_arg_locations;
21702 call_site_count = old_call_site_count;
21703 tail_call_site_count = old_tail_call_site_count;
21704}
21705
21706/* Helper function of premark_used_types() which gets called through
21707 htab_traverse.
21708
21709 Marks the DIE of a given type in *SLOT as perennial, so it never gets
21710 marked as unused by prune_unused_types. */
21711
21712bool
21713premark_used_types_helper (tree const &type, void *)
21714{
21715 dw_die_ref die;
21716
21717 die = lookup_type_die (type);
21718 if (die != NULL)
21719 die->die_perennial_p = 1;
21720 return true;
21721}
21722
21723/* Helper function of premark_types_used_by_global_vars which gets called
21724 through htab_traverse.
21725
21726 Marks the DIE of a given type in *SLOT as perennial, so it never gets
21727 marked as unused by prune_unused_types. The DIE of the type is marked
21728 only if the global variable using the type will actually be emitted. */
21729
21730int
21731premark_types_used_by_global_vars_helper (types_used_by_vars_entry **slot,
21732 void *)
21733{
21734 struct types_used_by_vars_entry *entry;
21735 dw_die_ref die;
21736
21737 entry = (struct types_used_by_vars_entry *) *slot;
21738 gcc_assert (entry->type != NULL
21739 && entry->var_decl != NULL);
21740 die = lookup_type_die (entry->type);
21741 if (die)
21742 {
21743 /* Ask cgraph if the global variable really is to be emitted.
21744 If yes, then we'll keep the DIE of ENTRY->TYPE. */
21745 varpool_node *node = varpool_node::get (entry->var_decl);
21746 if (node && node->definition)
21747 {
21748 die->die_perennial_p = 1;
21749 /* Keep the parent DIEs as well. */
21750 while ((die = die->die_parent) && die->die_perennial_p == 0)
21751 die->die_perennial_p = 1;
21752 }
21753 }
21754 return 1;
21755}
21756
21757/* Mark all members of used_types_hash as perennial. */
21758
21759static void
21760premark_used_types (struct function *fun)
21761{
21762 if (fun && fun->used_types_hash)
21763 fun->used_types_hash->traverse<void *, premark_used_types_helper> (NULL);
21764}
21765
21766/* Mark all members of types_used_by_vars_entry as perennial. */
21767
21768static void
21769premark_types_used_by_global_vars (void)
21770{
21771 if (types_used_by_vars_hash)
21772 types_used_by_vars_hash
21773 ->traverse<void *, premark_types_used_by_global_vars_helper> (NULL);
21774}
21775
21776/* Generate a DW_TAG_call_site DIE in function DECL under SUBR_DIE
21777 for CA_LOC call arg loc node. */
21778
21779static dw_die_ref
21780gen_call_site_die (tree decl, dw_die_ref subr_die,
21781 struct call_arg_loc_node *ca_loc)
21782{
21783 dw_die_ref stmt_die = NULL, die;
21784 tree block = ca_loc->block;
21785
21786 while (block
21787 && block != DECL_INITIAL (decl)
21788 && TREE_CODE (block) == BLOCK)
21789 {
21790 stmt_die = BLOCK_DIE (block);
21791 if (stmt_die)
21792 break;
21793 block = BLOCK_SUPERCONTEXT (block);
21794 }
21795 if (stmt_die == NULL)
21796 stmt_die = subr_die;
21797 die = new_die (dwarf_TAG (DW_TAG_call_site), stmt_die, NULL_TREE);
21798 add_AT_lbl_id (die, dwarf_AT (DW_AT_call_return_pc), ca_loc->label);
21799 if (ca_loc->tail_call_p)
21800 add_AT_flag (die, dwarf_AT (DW_AT_call_tail_call), 1);
21801 if (ca_loc->symbol_ref)
21802 {
21803 dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
21804 if (tdie)
21805 add_AT_die_ref (die, dwarf_AT (DW_AT_call_origin), tdie);
21806 else
21807 add_AT_addr (die, dwarf_AT (DW_AT_call_origin), ca_loc->symbol_ref,
21808 false);
21809 }
21810 return die;
21811}
21812
21813/* Generate a DIE to represent a declared function (either file-scope or
21814 block-local). */
21815
21816static void
21817gen_subprogram_die (tree decl, dw_die_ref context_die)
21818{
21819 tree origin = decl_ultimate_origin (decl);
21820 dw_die_ref subr_die;
21821 dw_die_ref old_die = lookup_decl_die (decl);
21822
21823 /* This function gets called multiple times for different stages of
21824 the debug process. For example, for func() in this code:
21825
21826 namespace S
21827 {
21828 void func() { ... }
21829 }
21830
21831 ...we get called 4 times. Twice in early debug and twice in
21832 late debug:
21833
21834 Early debug
21835 -----------
21836
21837 1. Once while generating func() within the namespace. This is
21838 the declaration. The declaration bit below is set, as the
21839 context is the namespace.
21840
21841 A new DIE will be generated with DW_AT_declaration set.
21842
21843 2. Once for func() itself. This is the specification. The
21844 declaration bit below is clear as the context is the CU.
21845
21846 We will use the cached DIE from (1) to create a new DIE with
21847 DW_AT_specification pointing to the declaration in (1).
21848
21849 Late debug via rest_of_handle_final()
21850 -------------------------------------
21851
21852 3. Once generating func() within the namespace. This is also the
21853 declaration, as in (1), but this time we will early exit below
21854 as we have a cached DIE and a declaration needs no additional
21855 annotations (no locations), as the source declaration line
21856 info is enough.
21857
21858 4. Once for func() itself. As in (2), this is the specification,
21859 but this time we will re-use the cached DIE, and just annotate
21860 it with the location information that should now be available.
21861
21862 For something without namespaces, but with abstract instances, we
21863 are also called a multiple times:
21864
21865 class Base
21866 {
21867 public:
21868 Base (); // constructor declaration (1)
21869 };
21870
21871 Base::Base () { } // constructor specification (2)
21872
21873 Early debug
21874 -----------
21875
21876 1. Once for the Base() constructor by virtue of it being a
21877 member of the Base class. This is done via
21878 rest_of_type_compilation.
21879
21880 This is a declaration, so a new DIE will be created with
21881 DW_AT_declaration.
21882
21883 2. Once for the Base() constructor definition, but this time
21884 while generating the abstract instance of the base
21885 constructor (__base_ctor) which is being generated via early
21886 debug of reachable functions.
21887
21888 Even though we have a cached version of the declaration (1),
21889 we will create a DW_AT_specification of the declaration DIE
21890 in (1).
21891
21892 3. Once for the __base_ctor itself, but this time, we generate
21893 an DW_AT_abstract_origin version of the DW_AT_specification in
21894 (2).
21895
21896 Late debug via rest_of_handle_final
21897 -----------------------------------
21898
21899 4. One final time for the __base_ctor (which will have a cached
21900 DIE with DW_AT_abstract_origin created in (3). This time,
21901 we will just annotate the location information now
21902 available.
21903 */
21904 int declaration = (current_function_decl != decl
21905 || class_or_namespace_scope_p (context_die));
21906
21907 /* Now that the C++ front end lazily declares artificial member fns, we
21908 might need to retrofit the declaration into its class. */
21909 if (!declaration && !origin && !old_die
21910 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
21911 && !class_or_namespace_scope_p (context_die)
21912 && debug_info_level > DINFO_LEVEL_TERSE)
21913 old_die = force_decl_die (decl);
21914
21915 /* An inlined instance, tag a new DIE with DW_AT_abstract_origin. */
21916 if (origin != NULL)
21917 {
21918 gcc_assert (!declaration || local_scope_p (context_die));
21919
21920 /* Fixup die_parent for the abstract instance of a nested
21921 inline function. */
21922 if (old_die && old_die->die_parent == NULL)
21923 add_child_die (context_die, old_die);
21924
21925 if (old_die && get_AT_ref (old_die, DW_AT_abstract_origin))
21926 {
21927 /* If we have a DW_AT_abstract_origin we have a working
21928 cached version. */
21929 subr_die = old_die;
21930 }
21931 else
21932 {
21933 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
21934 add_abstract_origin_attribute (subr_die, origin);
21935 /* This is where the actual code for a cloned function is.
21936 Let's emit linkage name attribute for it. This helps
21937 debuggers to e.g, set breakpoints into
21938 constructors/destructors when the user asks "break
21939 K::K". */
21940 add_linkage_name (subr_die, decl);
21941 }
21942 }
21943 /* A cached copy, possibly from early dwarf generation. Reuse as
21944 much as possible. */
21945 else if (old_die)
21946 {
21947 /* A declaration that has been previously dumped needs no
21948 additional information. */
21949 if (declaration)
21950 return;
21951
21952 if (!get_AT_flag (old_die, DW_AT_declaration)
21953 /* We can have a normal definition following an inline one in the
21954 case of redefinition of GNU C extern inlines.
21955 It seems reasonable to use AT_specification in this case. */
21956 && !get_AT (old_die, DW_AT_inline))
21957 {
21958 /* Detect and ignore this case, where we are trying to output
21959 something we have already output. */
21960 if (get_AT (old_die, DW_AT_low_pc)
21961 || get_AT (old_die, DW_AT_ranges))
21962 return;
21963
21964 /* If we have no location information, this must be a
21965 partially generated DIE from early dwarf generation.
21966 Fall through and generate it. */
21967 }
21968
21969 /* If the definition comes from the same place as the declaration,
21970 maybe use the old DIE. We always want the DIE for this function
21971 that has the *_pc attributes to be under comp_unit_die so the
21972 debugger can find it. We also need to do this for abstract
21973 instances of inlines, since the spec requires the out-of-line copy
21974 to have the same parent. For local class methods, this doesn't
21975 apply; we just use the old DIE. */
21976 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
21977 struct dwarf_file_data * file_index = lookup_filename (s.file);
21978 if ((is_cu_die (old_die->die_parent)
21979 /* This condition fixes the inconsistency/ICE with the
21980 following Fortran test (or some derivative thereof) while
21981 building libgfortran:
21982
21983 module some_m
21984 contains
21985 logical function funky (FLAG)
21986 funky = .true.
21987 end function
21988 end module
21989 */
21990 || (old_die->die_parent
21991 && old_die->die_parent->die_tag == DW_TAG_module)
21992 || context_die == NULL)
21993 && (DECL_ARTIFICIAL (decl)
21994 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
21995 && (get_AT_unsigned (old_die, DW_AT_decl_line)
21996 == (unsigned) s.line)
21997 && (!debug_column_info
21998 || s.column == 0
21999 || (get_AT_unsigned (old_die, DW_AT_decl_column)
22000 == (unsigned) s.column)))))
22001 {
22002 subr_die = old_die;
22003
22004 /* Clear out the declaration attribute, but leave the
22005 parameters so they can be augmented with location
22006 information later. Unless this was a declaration, in
22007 which case, wipe out the nameless parameters and recreate
22008 them further down. */
22009 if (remove_AT (subr_die, DW_AT_declaration))
22010 {
22011
22012 remove_AT (subr_die, DW_AT_object_pointer);
22013 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
22014 }
22015 }
22016 /* Make a specification pointing to the previously built
22017 declaration. */
22018 else
22019 {
22020 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
22021 add_AT_specification (subr_die, old_die);
22022 add_pubname (decl, subr_die);
22023 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
22024 add_AT_file (subr_die, DW_AT_decl_file, file_index);
22025 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
22026 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
22027 if (debug_column_info
22028 && s.column
22029 && (get_AT_unsigned (old_die, DW_AT_decl_column)
22030 != (unsigned) s.column))
22031 add_AT_unsigned (subr_die, DW_AT_decl_column, s.column);
22032
22033 /* If the prototype had an 'auto' or 'decltype(auto)' return type,
22034 emit the real type on the definition die. */
22035 if (is_cxx () && debug_info_level > DINFO_LEVEL_TERSE)
22036 {
22037 dw_die_ref die = get_AT_ref (old_die, DW_AT_type);
22038 if (die == auto_die || die == decltype_auto_die)
22039 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
22040 TYPE_UNQUALIFIED, false, context_die);
22041 }
22042
22043 /* When we process the method declaration, we haven't seen
22044 the out-of-class defaulted definition yet, so we have to
22045 recheck now. */
22046 if ((dwarf_version >= 5 || ! dwarf_strict)
22047 && !get_AT (subr_die, DW_AT_defaulted))
22048 {
22049 int defaulted
22050 = lang_hooks.decls.decl_dwarf_attribute (decl,
22051 DW_AT_defaulted);
22052 if (defaulted != -1)
22053 {
22054 /* Other values must have been handled before. */
22055 gcc_assert (defaulted == DW_DEFAULTED_out_of_class);
22056 add_AT_unsigned (subr_die, DW_AT_defaulted, defaulted);
22057 }
22058 }
22059 }
22060 }
22061 /* Create a fresh DIE for anything else. */
22062 else
22063 {
22064 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
22065
22066 if (TREE_PUBLIC (decl))
22067 add_AT_flag (subr_die, DW_AT_external, 1);
22068
22069 add_name_and_src_coords_attributes (subr_die, decl);
22070 add_pubname (decl, subr_die);
22071 if (debug_info_level > DINFO_LEVEL_TERSE)
22072 {
22073 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
22074 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
22075 TYPE_UNQUALIFIED, false, context_die);
22076 }
22077
22078 add_pure_or_virtual_attribute (subr_die, decl);
22079 if (DECL_ARTIFICIAL (decl))
22080 add_AT_flag (subr_die, DW_AT_artificial, 1);
22081
22082 if (TREE_THIS_VOLATILE (decl) && (dwarf_version >= 5 || !dwarf_strict))
22083 add_AT_flag (subr_die, DW_AT_noreturn, 1);
22084
22085 add_alignment_attribute (subr_die, decl);
22086
22087 add_accessibility_attribute (subr_die, decl);
22088 }
22089
22090 /* Unless we have an existing non-declaration DIE, equate the new
22091 DIE. */
22092 if (!old_die || is_declaration_die (old_die))
22093 equate_decl_number_to_die (decl, subr_die);
22094
22095 if (declaration)
22096 {
22097 if (!old_die || !get_AT (old_die, DW_AT_inline))
22098 {
22099 add_AT_flag (subr_die, DW_AT_declaration, 1);
22100
22101 /* If this is an explicit function declaration then generate
22102 a DW_AT_explicit attribute. */
22103 if ((dwarf_version >= 3 || !dwarf_strict)
22104 && lang_hooks.decls.decl_dwarf_attribute (decl,
22105 DW_AT_explicit) == 1)
22106 add_AT_flag (subr_die, DW_AT_explicit, 1);
22107
22108 /* If this is a C++11 deleted special function member then generate
22109 a DW_AT_deleted attribute. */
22110 if ((dwarf_version >= 5 || !dwarf_strict)
22111 && lang_hooks.decls.decl_dwarf_attribute (decl,
22112 DW_AT_deleted) == 1)
22113 add_AT_flag (subr_die, DW_AT_deleted, 1);
22114
22115 /* If this is a C++11 defaulted special function member then
22116 generate a DW_AT_defaulted attribute. */
22117 if (dwarf_version >= 5 || !dwarf_strict)
22118 {
22119 int defaulted
22120 = lang_hooks.decls.decl_dwarf_attribute (decl,
22121 DW_AT_defaulted);
22122 if (defaulted != -1)
22123 add_AT_unsigned (subr_die, DW_AT_defaulted, defaulted);
22124 }
22125
22126 /* If this is a C++11 non-static member function with & ref-qualifier
22127 then generate a DW_AT_reference attribute. */
22128 if ((dwarf_version >= 5 || !dwarf_strict)
22129 && lang_hooks.decls.decl_dwarf_attribute (decl,
22130 DW_AT_reference) == 1)
22131 add_AT_flag (subr_die, DW_AT_reference, 1);
22132
22133 /* If this is a C++11 non-static member function with &&
22134 ref-qualifier then generate a DW_AT_reference attribute. */
22135 if ((dwarf_version >= 5 || !dwarf_strict)
22136 && lang_hooks.decls.decl_dwarf_attribute (decl,
22137 DW_AT_rvalue_reference)
22138 == 1)
22139 add_AT_flag (subr_die, DW_AT_rvalue_reference, 1);
22140 }
22141 }
22142 /* Tag abstract instances with DW_AT_inline. */
22143 else if (DECL_ABSTRACT_P (decl))
22144 {
22145 if (DECL_DECLARED_INLINE_P (decl))
22146 {
22147 if (cgraph_function_possibly_inlined_p (decl))
22148 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
22149 else
22150 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
22151 }
22152 else
22153 {
22154 if (cgraph_function_possibly_inlined_p (decl))
22155 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
22156 else
22157 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
22158 }
22159
22160 if (DECL_DECLARED_INLINE_P (decl)
22161 && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
22162 add_AT_flag (subr_die, DW_AT_artificial, 1);
22163 }
22164 /* For non DECL_EXTERNALs, if range information is available, fill
22165 the DIE with it. */
22166 else if (!DECL_EXTERNAL (decl) && !early_dwarf)
22167 {
22168 HOST_WIDE_INT cfa_fb_offset;
22169
22170 struct function *fun = DECL_STRUCT_FUNCTION (decl);
22171
22172 if (!flag_reorder_blocks_and_partition)
22173 {
22174 dw_fde_ref fde = fun->fde;
22175 if (fde->dw_fde_begin)
22176 {
22177 /* We have already generated the labels. */
22178 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
22179 fde->dw_fde_end, false);
22180 }
22181 else
22182 {
22183 /* Create start/end labels and add the range. */
22184 char label_id_low[MAX_ARTIFICIAL_LABEL_BYTES];
22185 char label_id_high[MAX_ARTIFICIAL_LABEL_BYTES];
22186 ASM_GENERATE_INTERNAL_LABEL (label_id_low, FUNC_BEGIN_LABEL,
22187 current_function_funcdef_no);
22188 ASM_GENERATE_INTERNAL_LABEL (label_id_high, FUNC_END_LABEL,
22189 current_function_funcdef_no);
22190 add_AT_low_high_pc (subr_die, label_id_low, label_id_high,
22191 false);
22192 }
22193
22194#if VMS_DEBUGGING_INFO
22195 /* HP OpenVMS Industry Standard 64: DWARF Extensions
22196 Section 2.3 Prologue and Epilogue Attributes:
22197 When a breakpoint is set on entry to a function, it is generally
22198 desirable for execution to be suspended, not on the very first
22199 instruction of the function, but rather at a point after the
22200 function's frame has been set up, after any language defined local
22201 declaration processing has been completed, and before execution of
22202 the first statement of the function begins. Debuggers generally
22203 cannot properly determine where this point is. Similarly for a
22204 breakpoint set on exit from a function. The prologue and epilogue
22205 attributes allow a compiler to communicate the location(s) to use. */
22206
22207 {
22208 if (fde->dw_fde_vms_end_prologue)
22209 add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
22210 fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
22211
22212 if (fde->dw_fde_vms_begin_epilogue)
22213 add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
22214 fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
22215 }
22216#endif
22217
22218 }
22219 else
22220 {
22221 /* Generate pubnames entries for the split function code ranges. */
22222 dw_fde_ref fde = fun->fde;
22223
22224 if (fde->dw_fde_second_begin)
22225 {
22226 if (dwarf_version >= 3 || !dwarf_strict)
22227 {
22228 /* We should use ranges for non-contiguous code section
22229 addresses. Use the actual code range for the initial
22230 section, since the HOT/COLD labels might precede an
22231 alignment offset. */
22232 bool range_list_added = false;
22233 add_ranges_by_labels (subr_die, fde->dw_fde_begin,
22234 fde->dw_fde_end, &range_list_added,
22235 false);
22236 add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
22237 fde->dw_fde_second_end,
22238 &range_list_added, false);
22239 if (range_list_added)
22240 add_ranges (NULL);
22241 }
22242 else
22243 {
22244 /* There is no real support in DW2 for this .. so we make
22245 a work-around. First, emit the pub name for the segment
22246 containing the function label. Then make and emit a
22247 simplified subprogram DIE for the second segment with the
22248 name pre-fixed by __hot/cold_sect_of_. We use the same
22249 linkage name for the second die so that gdb will find both
22250 sections when given "b foo". */
22251 const char *name = NULL;
22252 tree decl_name = DECL_NAME (decl);
22253 dw_die_ref seg_die;
22254
22255 /* Do the 'primary' section. */
22256 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
22257 fde->dw_fde_end, false);
22258
22259 /* Build a minimal DIE for the secondary section. */
22260 seg_die = new_die (DW_TAG_subprogram,
22261 subr_die->die_parent, decl);
22262
22263 if (TREE_PUBLIC (decl))
22264 add_AT_flag (seg_die, DW_AT_external, 1);
22265
22266 if (decl_name != NULL
22267 && IDENTIFIER_POINTER (decl_name) != NULL)
22268 {
22269 name = dwarf2_name (decl, 1);
22270 if (! DECL_ARTIFICIAL (decl))
22271 add_src_coords_attributes (seg_die, decl);
22272
22273 add_linkage_name (seg_die, decl);
22274 }
22275 gcc_assert (name != NULL);
22276 add_pure_or_virtual_attribute (seg_die, decl);
22277 if (DECL_ARTIFICIAL (decl))
22278 add_AT_flag (seg_die, DW_AT_artificial, 1);
22279
22280 name = concat ("__second_sect_of_", name, NULL);
22281 add_AT_low_high_pc (seg_die, fde->dw_fde_second_begin,
22282 fde->dw_fde_second_end, false);
22283 add_name_attribute (seg_die, name);
22284 if (want_pubnames ())
22285 add_pubname_string (name, seg_die);
22286 }
22287 }
22288 else
22289 add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end,
22290 false);
22291 }
22292
22293 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
22294
22295 /* We define the "frame base" as the function's CFA. This is more
22296 convenient for several reasons: (1) It's stable across the prologue
22297 and epilogue, which makes it better than just a frame pointer,
22298 (2) With dwarf3, there exists a one-byte encoding that allows us
22299 to reference the .debug_frame data by proxy, but failing that,
22300 (3) We can at least reuse the code inspection and interpretation
22301 code that determines the CFA position at various points in the
22302 function. */
22303 if (dwarf_version >= 3 && targetm.debug_unwind_info () == UI_DWARF2)
22304 {
22305 dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
22306 add_AT_loc (subr_die, DW_AT_frame_base, op);
22307 }
22308 else
22309 {
22310 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
22311 if (list->dw_loc_next)
22312 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
22313 else
22314 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
22315 }
22316
22317 /* Compute a displacement from the "steady-state frame pointer" to
22318 the CFA. The former is what all stack slots and argument slots
22319 will reference in the rtl; the latter is what we've told the
22320 debugger about. We'll need to adjust all frame_base references
22321 by this displacement. */
22322 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
22323
22324 if (fun->static_chain_decl)
22325 {
22326 /* DWARF requires here a location expression that computes the
22327 address of the enclosing subprogram's frame base. The machinery
22328 in tree-nested.c is supposed to store this specific address in the
22329 last field of the FRAME record. */
22330 const tree frame_type
22331 = TREE_TYPE (TREE_TYPE (fun->static_chain_decl));
22332 const tree fb_decl = tree_last (TYPE_FIELDS (frame_type));
22333
22334 tree fb_expr
22335 = build1 (INDIRECT_REF, frame_type, fun->static_chain_decl);
22336 fb_expr = build3 (COMPONENT_REF, TREE_TYPE (fb_decl),
22337 fb_expr, fb_decl, NULL_TREE);
22338
22339 add_AT_location_description (subr_die, DW_AT_static_link,
22340 loc_list_from_tree (fb_expr, 0, NULL));
22341 }
22342
22343 resolve_variable_values ();
22344 }
22345
22346 /* Generate child dies for template paramaters. */
22347 if (early_dwarf && debug_info_level > DINFO_LEVEL_TERSE)
22348 gen_generic_params_dies (decl);
22349
22350 /* Now output descriptions of the arguments for this function. This gets
22351 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
22352 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
22353 `...' at the end of the formal parameter list. In order to find out if
22354 there was a trailing ellipsis or not, we must instead look at the type
22355 associated with the FUNCTION_DECL. This will be a node of type
22356 FUNCTION_TYPE. If the chain of type nodes hanging off of this
22357 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
22358 an ellipsis at the end. */
22359
22360 /* In the case where we are describing a mere function declaration, all we
22361 need to do here (and all we *can* do here) is to describe the *types* of
22362 its formal parameters. */
22363 if (debug_info_level <= DINFO_LEVEL_TERSE)
22364 ;
22365 else if (declaration)
22366 gen_formal_types_die (decl, subr_die);
22367 else
22368 {
22369 /* Generate DIEs to represent all known formal parameters. */
22370 tree parm = DECL_ARGUMENTS (decl);
22371 tree generic_decl = early_dwarf
22372 ? lang_hooks.decls.get_generic_function_decl (decl) : NULL;
22373 tree generic_decl_parm = generic_decl
22374 ? DECL_ARGUMENTS (generic_decl)
22375 : NULL;
22376
22377 /* Now we want to walk the list of parameters of the function and
22378 emit their relevant DIEs.
22379
22380 We consider the case of DECL being an instance of a generic function
22381 as well as it being a normal function.
22382
22383 If DECL is an instance of a generic function we walk the
22384 parameters of the generic function declaration _and_ the parameters of
22385 DECL itself. This is useful because we want to emit specific DIEs for
22386 function parameter packs and those are declared as part of the
22387 generic function declaration. In that particular case,
22388 the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
22389 That DIE has children DIEs representing the set of arguments
22390 of the pack. Note that the set of pack arguments can be empty.
22391 In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
22392 children DIE.
22393
22394 Otherwise, we just consider the parameters of DECL. */
22395 while (generic_decl_parm || parm)
22396 {
22397 if (generic_decl_parm
22398 && lang_hooks.function_parameter_pack_p (generic_decl_parm))
22399 gen_formal_parameter_pack_die (generic_decl_parm,
22400 parm, subr_die,
22401 &parm);
22402 else if (parm && !POINTER_BOUNDS_P (parm))
22403 {
22404 dw_die_ref parm_die = gen_decl_die (parm, NULL, NULL, subr_die);
22405
22406 if (parm == DECL_ARGUMENTS (decl)
22407 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
22408 && parm_die
22409 && (dwarf_version >= 3 || !dwarf_strict))
22410 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
22411
22412 parm = DECL_CHAIN (parm);
22413 }
22414 else if (parm)
22415 parm = DECL_CHAIN (parm);
22416
22417 if (generic_decl_parm)
22418 generic_decl_parm = DECL_CHAIN (generic_decl_parm);
22419 }
22420
22421 /* Decide whether we need an unspecified_parameters DIE at the end.
22422 There are 2 more cases to do this for: 1) the ansi ... declaration -
22423 this is detectable when the end of the arg list is not a
22424 void_type_node 2) an unprototyped function declaration (not a
22425 definition). This just means that we have no info about the
22426 parameters at all. */
22427 if (early_dwarf)
22428 {
22429 if (prototype_p (TREE_TYPE (decl)))
22430 {
22431 /* This is the prototyped case, check for.... */
22432 if (stdarg_p (TREE_TYPE (decl)))
22433 gen_unspecified_parameters_die (decl, subr_die);
22434 }
22435 else if (DECL_INITIAL (decl) == NULL_TREE)
22436 gen_unspecified_parameters_die (decl, subr_die);
22437 }
22438 }
22439
22440 if (subr_die != old_die)
22441 /* Add the calling convention attribute if requested. */
22442 add_calling_convention_attribute (subr_die, decl);
22443
22444 /* Output Dwarf info for all of the stuff within the body of the function
22445 (if it has one - it may be just a declaration).
22446
22447 OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
22448 a function. This BLOCK actually represents the outermost binding contour
22449 for the function, i.e. the contour in which the function's formal
22450 parameters and labels get declared. Curiously, it appears that the front
22451 end doesn't actually put the PARM_DECL nodes for the current function onto
22452 the BLOCK_VARS list for this outer scope, but are strung off of the
22453 DECL_ARGUMENTS list for the function instead.
22454
22455 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
22456 the LABEL_DECL nodes for the function however, and we output DWARF info
22457 for those in decls_for_scope. Just within the `outer_scope' there will be
22458 a BLOCK node representing the function's outermost pair of curly braces,
22459 and any blocks used for the base and member initializers of a C++
22460 constructor function. */
22461 tree outer_scope = DECL_INITIAL (decl);
22462 if (! declaration && outer_scope && TREE_CODE (outer_scope) != ERROR_MARK)
22463 {
22464 int call_site_note_count = 0;
22465 int tail_call_site_note_count = 0;
22466
22467 /* Emit a DW_TAG_variable DIE for a named return value. */
22468 if (DECL_NAME (DECL_RESULT (decl)))
22469 gen_decl_die (DECL_RESULT (decl), NULL, NULL, subr_die);
22470
22471 /* The first time through decls_for_scope we will generate the
22472 DIEs for the locals. The second time, we fill in the
22473 location info. */
22474 decls_for_scope (outer_scope, subr_die);
22475
22476 if (call_arg_locations && (!dwarf_strict || dwarf_version >= 5))
22477 {
22478 struct call_arg_loc_node *ca_loc;
22479 for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
22480 {
22481 dw_die_ref die = NULL;
22482 rtx tloc = NULL_RTX, tlocc = NULL_RTX;
22483 rtx arg, next_arg;
22484
22485 for (arg = (ca_loc->call_arg_loc_note != NULL_RTX
22486 ? NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note)
22487 : NULL_RTX);
22488 arg; arg = next_arg)
22489 {
22490 dw_loc_descr_ref reg, val;
22491 machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
22492 dw_die_ref cdie, tdie = NULL;
22493
22494 next_arg = XEXP (arg, 1);
22495 if (REG_P (XEXP (XEXP (arg, 0), 0))
22496 && next_arg
22497 && MEM_P (XEXP (XEXP (next_arg, 0), 0))
22498 && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
22499 && REGNO (XEXP (XEXP (arg, 0), 0))
22500 == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
22501 next_arg = XEXP (next_arg, 1);
22502 if (mode == VOIDmode)
22503 {
22504 mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
22505 if (mode == VOIDmode)
22506 mode = GET_MODE (XEXP (arg, 0));
22507 }
22508 if (mode == VOIDmode || mode == BLKmode)
22509 continue;
22510 /* Get dynamic information about call target only if we
22511 have no static information: we cannot generate both
22512 DW_AT_call_origin and DW_AT_call_target
22513 attributes. */
22514 if (ca_loc->symbol_ref == NULL_RTX)
22515 {
22516 if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
22517 {
22518 tloc = XEXP (XEXP (arg, 0), 1);
22519 continue;
22520 }
22521 else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
22522 && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
22523 {
22524 tlocc = XEXP (XEXP (arg, 0), 1);
22525 continue;
22526 }
22527 }
22528 reg = NULL;
22529 if (REG_P (XEXP (XEXP (arg, 0), 0)))
22530 reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
22531 VAR_INIT_STATUS_INITIALIZED);
22532 else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
22533 {
22534 rtx mem = XEXP (XEXP (arg, 0), 0);
22535 reg = mem_loc_descriptor (XEXP (mem, 0),
22536 get_address_mode (mem),
22537 GET_MODE (mem),
22538 VAR_INIT_STATUS_INITIALIZED);
22539 }
22540 else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
22541 == DEBUG_PARAMETER_REF)
22542 {
22543 tree tdecl
22544 = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
22545 tdie = lookup_decl_die (tdecl);
22546 if (tdie == NULL)
22547 continue;
22548 }
22549 else
22550 continue;
22551 if (reg == NULL
22552 && GET_CODE (XEXP (XEXP (arg, 0), 0))
22553 != DEBUG_PARAMETER_REF)
22554 continue;
22555 val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
22556 VOIDmode,
22557 VAR_INIT_STATUS_INITIALIZED);
22558 if (val == NULL)
22559 continue;
22560 if (die == NULL)
22561 die = gen_call_site_die (decl, subr_die, ca_loc);
22562 cdie = new_die (dwarf_TAG (DW_TAG_call_site_parameter), die,
22563 NULL_TREE);
22564 if (reg != NULL)
22565 add_AT_loc (cdie, DW_AT_location, reg);
22566 else if (tdie != NULL)
22567 add_AT_die_ref (cdie, dwarf_AT (DW_AT_call_parameter),
22568 tdie);
22569 add_AT_loc (cdie, dwarf_AT (DW_AT_call_value), val);
22570 if (next_arg != XEXP (arg, 1))
22571 {
22572 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
22573 if (mode == VOIDmode)
22574 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
22575 val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
22576 0), 1),
22577 mode, VOIDmode,
22578 VAR_INIT_STATUS_INITIALIZED);
22579 if (val != NULL)
22580 add_AT_loc (cdie, dwarf_AT (DW_AT_call_data_value),
22581 val);
22582 }
22583 }
22584 if (die == NULL
22585 && (ca_loc->symbol_ref || tloc))
22586 die = gen_call_site_die (decl, subr_die, ca_loc);
22587 if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
22588 {
22589 dw_loc_descr_ref tval = NULL;
22590
22591 if (tloc != NULL_RTX)
22592 tval = mem_loc_descriptor (tloc,
22593 GET_MODE (tloc) == VOIDmode
22594 ? Pmode : GET_MODE (tloc),
22595 VOIDmode,
22596 VAR_INIT_STATUS_INITIALIZED);
22597 if (tval)
22598 add_AT_loc (die, dwarf_AT (DW_AT_call_target), tval);
22599 else if (tlocc != NULL_RTX)
22600 {
22601 tval = mem_loc_descriptor (tlocc,
22602 GET_MODE (tlocc) == VOIDmode
22603 ? Pmode : GET_MODE (tlocc),
22604 VOIDmode,
22605 VAR_INIT_STATUS_INITIALIZED);
22606 if (tval)
22607 add_AT_loc (die,
22608 dwarf_AT (DW_AT_call_target_clobbered),
22609 tval);
22610 }
22611 }
22612 if (die != NULL)
22613 {
22614 call_site_note_count++;
22615 if (ca_loc->tail_call_p)
22616 tail_call_site_note_count++;
22617 }
22618 }
22619 }
22620 call_arg_locations = NULL;
22621 call_arg_loc_last = NULL;
22622 if (tail_call_site_count >= 0
22623 && tail_call_site_count == tail_call_site_note_count
22624 && (!dwarf_strict || dwarf_version >= 5))
22625 {
22626 if (call_site_count >= 0
22627 && call_site_count == call_site_note_count)
22628 add_AT_flag (subr_die, dwarf_AT (DW_AT_call_all_calls), 1);
22629 else
22630 add_AT_flag (subr_die, dwarf_AT (DW_AT_call_all_tail_calls), 1);
22631 }
22632 call_site_count = -1;
22633 tail_call_site_count = -1;
22634 }
22635
22636 /* Mark used types after we have created DIEs for the functions scopes. */
22637 premark_used_types (DECL_STRUCT_FUNCTION (decl));
22638}
22639
22640/* Returns a hash value for X (which really is a die_struct). */
22641
22642hashval_t
22643block_die_hasher::hash (die_struct *d)
22644{
22645 return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
22646}
22647
22648/* Return nonzero if decl_id and die_parent of die_struct X is the same
22649 as decl_id and die_parent of die_struct Y. */
22650
22651bool
22652block_die_hasher::equal (die_struct *x, die_struct *y)
22653{
22654 return x->decl_id == y->decl_id && x->die_parent == y->die_parent;
22655}
22656
22657/* Return TRUE if DECL, which may have been previously generated as
22658 OLD_DIE, is a candidate for a DW_AT_specification. DECLARATION is
22659 true if decl (or its origin) is either an extern declaration or a
22660 class/namespace scoped declaration.
22661
22662 The declare_in_namespace support causes us to get two DIEs for one
22663 variable, both of which are declarations. We want to avoid
22664 considering one to be a specification, so we must test for
22665 DECLARATION and DW_AT_declaration. */
22666static inline bool
22667decl_will_get_specification_p (dw_die_ref old_die, tree decl, bool declaration)
22668{
22669 return (old_die && TREE_STATIC (decl) && !declaration
22670 && get_AT_flag (old_die, DW_AT_declaration) == 1);
22671}
22672
22673/* Return true if DECL is a local static. */
22674
22675static inline bool
22676local_function_static (tree decl)
22677{
22678 gcc_assert (VAR_P (decl));
22679 return TREE_STATIC (decl)
22680 && DECL_CONTEXT (decl)
22681 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL;
22682}
22683
22684/* Generate a DIE to represent a declared data object.
22685 Either DECL or ORIGIN must be non-null. */
22686
22687static void
22688gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
22689{
22690 HOST_WIDE_INT off = 0;
22691 tree com_decl;
22692 tree decl_or_origin = decl ? decl : origin;
22693 tree ultimate_origin;
22694 dw_die_ref var_die;
22695 dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
22696 bool declaration = (DECL_EXTERNAL (decl_or_origin)
22697 || class_or_namespace_scope_p (context_die));
22698 bool specialization_p = false;
22699 bool no_linkage_name = false;
22700
22701 /* While C++ inline static data members have definitions inside of the
22702 class, force the first DIE to be a declaration, then let gen_member_die
22703 reparent it to the class context and call gen_variable_die again
22704 to create the outside of the class DIE for the definition. */
22705 if (!declaration
22706 && old_die == NULL
22707 && decl
22708 && DECL_CONTEXT (decl)
22709 && TYPE_P (DECL_CONTEXT (decl))
22710 && lang_hooks.decls.decl_dwarf_attribute (decl, DW_AT_inline) != -1)
22711 {
22712 declaration = true;
22713 if (dwarf_version < 5)
22714 no_linkage_name = true;
22715 }
22716
22717 ultimate_origin = decl_ultimate_origin (decl_or_origin);
22718 if (decl || ultimate_origin)
22719 origin = ultimate_origin;
22720 com_decl = fortran_common (decl_or_origin, &off);
22721
22722 /* Symbol in common gets emitted as a child of the common block, in the form
22723 of a data member. */
22724 if (com_decl)
22725 {
22726 dw_die_ref com_die;
22727 dw_loc_list_ref loc = NULL;
22728 die_node com_die_arg;
22729
22730 var_die = lookup_decl_die (decl_or_origin);
22731 if (var_die)
22732 {
22733 if (! early_dwarf && get_AT (var_die, DW_AT_location) == NULL)
22734 {
22735 loc = loc_list_from_tree (com_decl, off ? 1 : 2, NULL);
22736 if (loc)
22737 {
22738 if (off)
22739 {
22740 /* Optimize the common case. */
22741 if (single_element_loc_list_p (loc)
22742 && loc->expr->dw_loc_opc == DW_OP_addr
22743 && loc->expr->dw_loc_next == NULL
22744 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
22745 == SYMBOL_REF)
22746 {
22747 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
22748 loc->expr->dw_loc_oprnd1.v.val_addr
22749 = plus_constant (GET_MODE (x), x , off);
22750 }
22751 else
22752 loc_list_plus_const (loc, off);
22753 }
22754 add_AT_location_description (var_die, DW_AT_location, loc);
22755 remove_AT (var_die, DW_AT_declaration);
22756 }
22757 }
22758 return;
22759 }
22760
22761 if (common_block_die_table == NULL)
22762 common_block_die_table = hash_table<block_die_hasher>::create_ggc (10);
22763
22764 com_die_arg.decl_id = DECL_UID (com_decl);
22765 com_die_arg.die_parent = context_die;
22766 com_die = common_block_die_table->find (&com_die_arg);
22767 if (! early_dwarf)
22768 loc = loc_list_from_tree (com_decl, 2, NULL);
22769 if (com_die == NULL)
22770 {
22771 const char *cnam
22772 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
22773 die_node **slot;
22774
22775 com_die = new_die (DW_TAG_common_block, context_die, decl);
22776 add_name_and_src_coords_attributes (com_die, com_decl);
22777 if (loc)
22778 {
22779 add_AT_location_description (com_die, DW_AT_location, loc);
22780 /* Avoid sharing the same loc descriptor between
22781 DW_TAG_common_block and DW_TAG_variable. */
22782 loc = loc_list_from_tree (com_decl, 2, NULL);
22783 }
22784 else if (DECL_EXTERNAL (decl_or_origin))
22785 add_AT_flag (com_die, DW_AT_declaration, 1);
22786 if (want_pubnames ())
22787 add_pubname_string (cnam, com_die); /* ??? needed? */
22788 com_die->decl_id = DECL_UID (com_decl);
22789 slot = common_block_die_table->find_slot (com_die, INSERT);
22790 *slot = com_die;
22791 }
22792 else if (get_AT (com_die, DW_AT_location) == NULL && loc)
22793 {
22794 add_AT_location_description (com_die, DW_AT_location, loc);
22795 loc = loc_list_from_tree (com_decl, 2, NULL);
22796 remove_AT (com_die, DW_AT_declaration);
22797 }
22798 var_die = new_die (DW_TAG_variable, com_die, decl);
22799 add_name_and_src_coords_attributes (var_die, decl_or_origin);
22800 add_type_attribute (var_die, TREE_TYPE (decl_or_origin),
22801 decl_quals (decl_or_origin), false,
22802 context_die);
22803 add_alignment_attribute (var_die, decl);
22804 add_AT_flag (var_die, DW_AT_external, 1);
22805 if (loc)
22806 {
22807 if (off)
22808 {
22809 /* Optimize the common case. */
22810 if (single_element_loc_list_p (loc)
22811 && loc->expr->dw_loc_opc == DW_OP_addr
22812 && loc->expr->dw_loc_next == NULL
22813 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
22814 {
22815 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
22816 loc->expr->dw_loc_oprnd1.v.val_addr
22817 = plus_constant (GET_MODE (x), x, off);
22818 }
22819 else
22820 loc_list_plus_const (loc, off);
22821 }
22822 add_AT_location_description (var_die, DW_AT_location, loc);
22823 }
22824 else if (DECL_EXTERNAL (decl_or_origin))
22825 add_AT_flag (var_die, DW_AT_declaration, 1);
22826 if (decl)
22827 equate_decl_number_to_die (decl, var_die);
22828 return;
22829 }
22830
22831 if (old_die)
22832 {
22833 if (declaration)
22834 {
22835 /* A declaration that has been previously dumped, needs no
22836 further annotations, since it doesn't need location on
22837 the second pass. */
22838 return;
22839 }
22840 else if (decl_will_get_specification_p (old_die, decl, declaration)
22841 && !get_AT (old_die, DW_AT_specification))
22842 {
22843 /* Fall-thru so we can make a new variable die along with a
22844 DW_AT_specification. */
22845 }
22846 else if (origin && old_die->die_parent != context_die)
22847 {
22848 /* If we will be creating an inlined instance, we need a
22849 new DIE that will get annotated with
22850 DW_AT_abstract_origin. Clear things so we can get a
22851 new DIE. */
22852 gcc_assert (!DECL_ABSTRACT_P (decl));
22853 old_die = NULL;
22854 }
22855 else
22856 {
22857 /* If a DIE was dumped early, it still needs location info.
22858 Skip to where we fill the location bits. */
22859 var_die = old_die;
22860 goto gen_variable_die_location;
22861 }
22862 }
22863
22864 /* For static data members, the declaration in the class is supposed
22865 to have DW_TAG_member tag in DWARF{3,4} and we emit it for compatibility
22866 also in DWARF2; the specification should still be DW_TAG_variable
22867 referencing the DW_TAG_member DIE. */
22868 if (declaration && class_scope_p (context_die) && dwarf_version < 5)
22869 var_die = new_die (DW_TAG_member, context_die, decl);
22870 else
22871 var_die = new_die (DW_TAG_variable, context_die, decl);
22872
22873 if (origin != NULL)
22874 add_abstract_origin_attribute (var_die, origin);
22875
22876 /* Loop unrolling can create multiple blocks that refer to the same
22877 static variable, so we must test for the DW_AT_declaration flag.
22878
22879 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
22880 copy decls and set the DECL_ABSTRACT_P flag on them instead of
22881 sharing them.
22882
22883 ??? Duplicated blocks have been rewritten to use .debug_ranges. */
22884 else if (decl_will_get_specification_p (old_die, decl, declaration))
22885 {
22886 /* This is a definition of a C++ class level static. */
22887 add_AT_specification (var_die, old_die);
22888 specialization_p = true;
22889 if (DECL_NAME (decl))
22890 {
22891 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
22892 struct dwarf_file_data * file_index = lookup_filename (s.file);
22893
22894 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
22895 add_AT_file (var_die, DW_AT_decl_file, file_index);
22896
22897 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
22898 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
22899
22900 if (debug_column_info
22901 && s.column
22902 && (get_AT_unsigned (old_die, DW_AT_decl_column)
22903 != (unsigned) s.column))
22904 add_AT_unsigned (var_die, DW_AT_decl_column, s.column);
22905
22906 if (old_die->die_tag == DW_TAG_member)
22907 add_linkage_name (var_die, decl);
22908 }
22909 }
22910 else
22911 add_name_and_src_coords_attributes (var_die, decl, no_linkage_name);
22912
22913 if ((origin == NULL && !specialization_p)
22914 || (origin != NULL
22915 && !DECL_ABSTRACT_P (decl_or_origin)
22916 && variably_modified_type_p (TREE_TYPE (decl_or_origin),
22917 decl_function_context
22918 (decl_or_origin))))
22919 {
22920 tree type = TREE_TYPE (decl_or_origin);
22921
22922 if (decl_by_reference_p (decl_or_origin))
22923 add_type_attribute (var_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
22924 context_die);
22925 else
22926 add_type_attribute (var_die, type, decl_quals (decl_or_origin), false,
22927 context_die);
22928 }
22929
22930 if (origin == NULL && !specialization_p)
22931 {
22932 if (TREE_PUBLIC (decl))
22933 add_AT_flag (var_die, DW_AT_external, 1);
22934
22935 if (DECL_ARTIFICIAL (decl))
22936 add_AT_flag (var_die, DW_AT_artificial, 1);
22937
22938 add_alignment_attribute (var_die, decl);
22939
22940 add_accessibility_attribute (var_die, decl);
22941 }
22942
22943 if (declaration)
22944 add_AT_flag (var_die, DW_AT_declaration, 1);
22945
22946 if (decl && (DECL_ABSTRACT_P (decl)
22947 || !old_die || is_declaration_die (old_die)))
22948 equate_decl_number_to_die (decl, var_die);
22949
22950 gen_variable_die_location:
22951 if (! declaration
22952 && (! DECL_ABSTRACT_P (decl_or_origin)
22953 /* Local static vars are shared between all clones/inlines,
22954 so emit DW_AT_location on the abstract DIE if DECL_RTL is
22955 already set. */
22956 || (VAR_P (decl_or_origin)
22957 && TREE_STATIC (decl_or_origin)
22958 && DECL_RTL_SET_P (decl_or_origin))))
22959 {
22960 if (early_dwarf)
22961 add_pubname (decl_or_origin, var_die);
22962 else
22963 add_location_or_const_value_attribute (var_die, decl_or_origin,
22964 decl == NULL);
22965 }
22966 else
22967 tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
22968
22969 if ((dwarf_version >= 4 || !dwarf_strict)
22970 && lang_hooks.decls.decl_dwarf_attribute (decl_or_origin,
22971 DW_AT_const_expr) == 1
22972 && !get_AT (var_die, DW_AT_const_expr)
22973 && !specialization_p)
22974 add_AT_flag (var_die, DW_AT_const_expr, 1);
22975
22976 if (!dwarf_strict)
22977 {
22978 int inl = lang_hooks.decls.decl_dwarf_attribute (decl_or_origin,
22979 DW_AT_inline);
22980 if (inl != -1
22981 && !get_AT (var_die, DW_AT_inline)
22982 && !specialization_p)
22983 add_AT_unsigned (var_die, DW_AT_inline, inl);
22984 }
22985}
22986
22987/* Generate a DIE to represent a named constant. */
22988
22989static void
22990gen_const_die (tree decl, dw_die_ref context_die)
22991{
22992 dw_die_ref const_die;
22993 tree type = TREE_TYPE (decl);
22994
22995 const_die = lookup_decl_die (decl);
22996 if (const_die)
22997 return;
22998
22999 const_die = new_die (DW_TAG_constant, context_die, decl);
23000 equate_decl_number_to_die (decl, const_die);
23001 add_name_and_src_coords_attributes (const_die, decl);
23002 add_type_attribute (const_die, type, TYPE_QUAL_CONST, false, context_die);
23003 if (TREE_PUBLIC (decl))
23004 add_AT_flag (const_die, DW_AT_external, 1);
23005 if (DECL_ARTIFICIAL (decl))
23006 add_AT_flag (const_die, DW_AT_artificial, 1);
23007 tree_add_const_value_attribute_for_decl (const_die, decl);
23008}
23009
23010/* Generate a DIE to represent a label identifier. */
23011
23012static void
23013gen_label_die (tree decl, dw_die_ref context_die)
23014{
23015 tree origin = decl_ultimate_origin (decl);
23016 dw_die_ref lbl_die = lookup_decl_die (decl);
23017 rtx insn;
23018 char label[MAX_ARTIFICIAL_LABEL_BYTES];
23019
23020 if (!lbl_die)
23021 {
23022 lbl_die = new_die (DW_TAG_label, context_die, decl);
23023 equate_decl_number_to_die (decl, lbl_die);
23024
23025 if (origin != NULL)
23026 add_abstract_origin_attribute (lbl_die, origin);
23027 else
23028 add_name_and_src_coords_attributes (lbl_die, decl);
23029 }
23030
23031 if (DECL_ABSTRACT_P (decl))
23032 equate_decl_number_to_die (decl, lbl_die);
23033 else if (! early_dwarf)
23034 {
23035 insn = DECL_RTL_IF_SET (decl);
23036
23037 /* Deleted labels are programmer specified labels which have been
23038 eliminated because of various optimizations. We still emit them
23039 here so that it is possible to put breakpoints on them. */
23040 if (insn
23041 && (LABEL_P (insn)
23042 || ((NOTE_P (insn)
23043 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
23044 {
23045 /* When optimization is enabled (via -O) some parts of the compiler
23046 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
23047 represent source-level labels which were explicitly declared by
23048 the user. This really shouldn't be happening though, so catch
23049 it if it ever does happen. */
23050 gcc_assert (!as_a<rtx_insn *> (insn)->deleted ());
23051
23052 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
23053 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
23054 }
23055 else if (insn
23056 && NOTE_P (insn)
23057 && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
23058 && CODE_LABEL_NUMBER (insn) != -1)
23059 {
23060 ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
23061 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
23062 }
23063 }
23064}
23065
23066/* A helper function for gen_inlined_subroutine_die. Add source coordinate
23067 attributes to the DIE for a block STMT, to describe where the inlined
23068 function was called from. This is similar to add_src_coords_attributes. */
23069
23070static inline void
23071add_call_src_coords_attributes (tree stmt, dw_die_ref die)
23072{
23073 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
23074
23075 if (dwarf_version >= 3 || !dwarf_strict)
23076 {
23077 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
23078 add_AT_unsigned (die, DW_AT_call_line, s.line);
23079 if (debug_column_info && s.column)
23080 add_AT_unsigned (die, DW_AT_call_column, s.column);
23081 }
23082}
23083
23084
23085/* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
23086 Add low_pc and high_pc attributes to the DIE for a block STMT. */
23087
23088static inline void
23089add_high_low_attributes (tree stmt, dw_die_ref die)
23090{
23091 char label[MAX_ARTIFICIAL_LABEL_BYTES];
23092
23093 if (BLOCK_FRAGMENT_CHAIN (stmt)
23094 && (dwarf_version >= 3 || !dwarf_strict))
23095 {
23096 tree chain, superblock = NULL_TREE;
23097 dw_die_ref pdie;
23098 dw_attr_node *attr = NULL;
23099
23100 if (inlined_function_outer_scope_p (stmt))
23101 {
23102 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
23103 BLOCK_NUMBER (stmt));
23104 add_AT_lbl_id (die, DW_AT_entry_pc, label);
23105 }
23106
23107 /* Optimize duplicate .debug_ranges lists or even tails of
23108 lists. If this BLOCK has same ranges as its supercontext,
23109 lookup DW_AT_ranges attribute in the supercontext (and
23110 recursively so), verify that the ranges_table contains the
23111 right values and use it instead of adding a new .debug_range. */
23112 for (chain = stmt, pdie = die;
23113 BLOCK_SAME_RANGE (chain);
23114 chain = BLOCK_SUPERCONTEXT (chain))
23115 {
23116 dw_attr_node *new_attr;
23117
23118 pdie = pdie->die_parent;
23119 if (pdie == NULL)
23120 break;
23121 if (BLOCK_SUPERCONTEXT (chain) == NULL_TREE)
23122 break;
23123 new_attr = get_AT (pdie, DW_AT_ranges);
23124 if (new_attr == NULL
23125 || new_attr->dw_attr_val.val_class != dw_val_class_range_list)
23126 break;
23127 attr = new_attr;
23128 superblock = BLOCK_SUPERCONTEXT (chain);
23129 }
23130 if (attr != NULL
23131 && ((*ranges_table)[attr->dw_attr_val.v.val_offset].num
23132 == BLOCK_NUMBER (superblock))
23133 && BLOCK_FRAGMENT_CHAIN (superblock))
23134 {
23135 unsigned long off = attr->dw_attr_val.v.val_offset;
23136 unsigned long supercnt = 0, thiscnt = 0;
23137 for (chain = BLOCK_FRAGMENT_CHAIN (superblock);
23138 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
23139 {
23140 ++supercnt;
23141 gcc_checking_assert ((*ranges_table)[off + supercnt].num
23142 == BLOCK_NUMBER (chain));
23143 }
23144 gcc_checking_assert ((*ranges_table)[off + supercnt + 1].num == 0);
23145 for (chain = BLOCK_FRAGMENT_CHAIN (stmt);
23146 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
23147 ++thiscnt;
23148 gcc_assert (supercnt >= thiscnt);
23149 add_AT_range_list (die, DW_AT_ranges, off + supercnt - thiscnt,
23150 false);
23151 note_rnglist_head (off + supercnt - thiscnt);
23152 return;
23153 }
23154
23155 unsigned int offset = add_ranges (stmt, true);
23156 add_AT_range_list (die, DW_AT_ranges, offset, false);
23157 note_rnglist_head (offset);
23158
23159 bool prev_in_cold = BLOCK_IN_COLD_SECTION_P (stmt);
23160 chain = BLOCK_FRAGMENT_CHAIN (stmt);
23161 do
23162 {
23163 add_ranges (chain, prev_in_cold != BLOCK_IN_COLD_SECTION_P (chain));
23164 prev_in_cold = BLOCK_IN_COLD_SECTION_P (chain);
23165 chain = BLOCK_FRAGMENT_CHAIN (chain);
23166 }
23167 while (chain);
23168 add_ranges (NULL);
23169 }
23170 else
23171 {
23172 char label_high[MAX_ARTIFICIAL_LABEL_BYTES];
23173 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
23174 BLOCK_NUMBER (stmt));
23175 ASM_GENERATE_INTERNAL_LABEL (label_high, BLOCK_END_LABEL,
23176 BLOCK_NUMBER (stmt));
23177 add_AT_low_high_pc (die, label, label_high, false);
23178 }
23179}
23180
23181/* Generate a DIE for a lexical block. */
23182
23183static void
23184gen_lexical_block_die (tree stmt, dw_die_ref context_die)
23185{
23186 dw_die_ref old_die = BLOCK_DIE (stmt);
23187 dw_die_ref stmt_die = NULL;
23188 if (!old_die)
23189 {
23190 stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
23191 BLOCK_DIE (stmt) = stmt_die;
23192 }
23193
23194 if (BLOCK_ABSTRACT (stmt))
23195 {
23196 if (old_die)
23197 {
23198 /* This must have been generated early and it won't even
23199 need location information since it's a DW_AT_inline
23200 function. */
23201 if (flag_checking)
23202 for (dw_die_ref c = context_die; c; c = c->die_parent)
23203 if (c->die_tag == DW_TAG_inlined_subroutine
23204 || c->die_tag == DW_TAG_subprogram)
23205 {
23206 gcc_assert (get_AT (c, DW_AT_inline));
23207 break;
23208 }
23209 return;
23210 }
23211 }
23212 else if (BLOCK_ABSTRACT_ORIGIN (stmt))
23213 {
23214 /* If this is an inlined instance, create a new lexical die for
23215 anything below to attach DW_AT_abstract_origin to. */
23216 if (old_die)
23217 {
23218 stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
23219 BLOCK_DIE (stmt) = stmt_die;
23220 old_die = NULL;
23221 }
23222
23223 tree origin = block_ultimate_origin (stmt);
23224 if (origin != NULL_TREE && origin != stmt)
23225 add_abstract_origin_attribute (stmt_die, origin);
23226 }
23227
23228 if (old_die)
23229 stmt_die = old_die;
23230
23231 /* A non abstract block whose blocks have already been reordered
23232 should have the instruction range for this block. If so, set the
23233 high/low attributes. */
23234 if (!early_dwarf && !BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
23235 {
23236 gcc_assert (stmt_die);
23237 add_high_low_attributes (stmt, stmt_die);
23238 }
23239
23240 decls_for_scope (stmt, stmt_die);
23241}
23242
23243/* Generate a DIE for an inlined subprogram. */
23244
23245static void
23246gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die)
23247{
23248 tree decl;
23249
23250 /* The instance of function that is effectively being inlined shall not
23251 be abstract. */
23252 gcc_assert (! BLOCK_ABSTRACT (stmt));
23253
23254 decl = block_ultimate_origin (stmt);
23255
23256 /* Make sure any inlined functions are known to be inlineable. */
23257 gcc_checking_assert (DECL_ABSTRACT_P (decl)
23258 || cgraph_function_possibly_inlined_p (decl));
23259
23260 /* Emit info for the abstract instance first, if we haven't yet. We
23261 must emit this even if the block is abstract, otherwise when we
23262 emit the block below (or elsewhere), we may end up trying to emit
23263 a die whose origin die hasn't been emitted, and crashing. */
23264 dwarf2out_abstract_function (decl);
23265
23266 if (! BLOCK_ABSTRACT (stmt))
23267 {
23268 dw_die_ref subr_die
23269 = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
23270
23271 if (call_arg_locations)
23272 BLOCK_DIE (stmt) = subr_die;
23273 add_abstract_origin_attribute (subr_die, decl);
23274 if (TREE_ASM_WRITTEN (stmt))
23275 add_high_low_attributes (stmt, subr_die);
23276 add_call_src_coords_attributes (stmt, subr_die);
23277
23278 decls_for_scope (stmt, subr_die);
23279 }
23280}
23281
23282/* Generate a DIE for a field in a record, or structure. CTX is required: see
23283 the comment for VLR_CONTEXT. */
23284
23285static void
23286gen_field_die (tree decl, struct vlr_context *ctx, dw_die_ref context_die)
23287{
23288 dw_die_ref decl_die;
23289
23290 if (TREE_TYPE (decl) == error_mark_node)
23291 return;
23292
23293 decl_die = new_die (DW_TAG_member, context_die, decl);
23294 add_name_and_src_coords_attributes (decl_die, decl);
23295 add_type_attribute (decl_die, member_declared_type (decl), decl_quals (decl),
23296 TYPE_REVERSE_STORAGE_ORDER (DECL_FIELD_CONTEXT (decl)),
23297 context_die);
23298
23299 if (DECL_BIT_FIELD_TYPE (decl))
23300 {
23301 add_byte_size_attribute (decl_die, decl);
23302 add_bit_size_attribute (decl_die, decl);
23303 add_bit_offset_attribute (decl_die, decl, ctx);
23304 }
23305
23306 add_alignment_attribute (decl_die, decl);
23307
23308 /* If we have a variant part offset, then we are supposed to process a member
23309 of a QUAL_UNION_TYPE, which is how we represent variant parts in
23310 trees. */
23311 gcc_assert (ctx->variant_part_offset == NULL_TREE
23312 || TREE_CODE (DECL_FIELD_CONTEXT (decl)) != QUAL_UNION_TYPE);
23313 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
23314 add_data_member_location_attribute (decl_die, decl, ctx);
23315
23316 if (DECL_ARTIFICIAL (decl))
23317 add_AT_flag (decl_die, DW_AT_artificial, 1);
23318
23319 add_accessibility_attribute (decl_die, decl);
23320
23321 /* Equate decl number to die, so that we can look up this decl later on. */
23322 equate_decl_number_to_die (decl, decl_die);
23323}
23324
23325#if 0
23326/* Don't generate either pointer_type DIEs or reference_type DIEs here.
23327 Use modified_type_die instead.
23328 We keep this code here just in case these types of DIEs may be needed to
23329 represent certain things in other languages (e.g. Pascal) someday. */
23330
23331static void
23332gen_pointer_type_die (tree type, dw_die_ref context_die)
23333{
23334 dw_die_ref ptr_die
23335 = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
23336
23337 equate_type_number_to_die (type, ptr_die);
23338 add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
23339 context_die);
23340 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
23341}
23342
23343/* Don't generate either pointer_type DIEs or reference_type DIEs here.
23344 Use modified_type_die instead.
23345 We keep this code here just in case these types of DIEs may be needed to
23346 represent certain things in other languages (e.g. Pascal) someday. */
23347
23348static void
23349gen_reference_type_die (tree type, dw_die_ref context_die)
23350{
23351 dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
23352
23353 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
23354 ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
23355 else
23356 ref_die = new_die (DW_TAG_reference_type, scope_die, type);
23357
23358 equate_type_number_to_die (type, ref_die);
23359 add_type_attribute (ref_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
23360 context_die);
23361 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
23362}
23363#endif
23364
23365/* Generate a DIE for a pointer to a member type. TYPE can be an
23366 OFFSET_TYPE, for a pointer to data member, or a RECORD_TYPE, for a
23367 pointer to member function. */
23368
23369static void
23370gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
23371{
23372 if (lookup_type_die (type))
23373 return;
23374
23375 dw_die_ref ptr_die = new_die (DW_TAG_ptr_to_member_type,
23376 scope_die_for (type, context_die), type);
23377
23378 equate_type_number_to_die (type, ptr_die);
23379 add_AT_die_ref (ptr_die, DW_AT_containing_type,
23380 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
23381 add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
23382 context_die);
23383 add_alignment_attribute (ptr_die, type);
23384
23385 if (TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE
23386 && TREE_CODE (TREE_TYPE (type)) != METHOD_TYPE)
23387 {
23388 dw_loc_descr_ref op = new_loc_descr (DW_OP_plus, 0, 0);
23389 add_AT_loc (ptr_die, DW_AT_use_location, op);
23390 }
23391}
23392
23393static char *producer_string;
23394
23395/* Return a heap allocated producer string including command line options
23396 if -grecord-gcc-switches. */
23397
23398static char *
23399gen_producer_string (void)
23400{
23401 size_t j;
23402 auto_vec<const char *> switches;
23403 const char *language_string = lang_hooks.name;
23404 char *producer, *tail;
23405 const char *p;
23406 size_t len = dwarf_record_gcc_switches ? 0 : 3;
23407 size_t plen = strlen (language_string) + 1 + strlen (version_string);
23408
23409 for (j = 1; dwarf_record_gcc_switches && j < save_decoded_options_count; j++)
23410 switch (save_decoded_options[j].opt_index)
23411 {
23412 case OPT_o:
23413 case OPT_d:
23414 case OPT_dumpbase:
23415 case OPT_dumpdir:
23416 case OPT_auxbase:
23417 case OPT_auxbase_strip:
23418 case OPT_quiet:
23419 case OPT_version:
23420 case OPT_v:
23421 case OPT_w:
23422 case OPT_L:
23423 case OPT_D:
23424 case OPT_I:
23425 case OPT_U:
23426 case OPT_SPECIAL_unknown:
23427 case OPT_SPECIAL_ignore:
23428 case OPT_SPECIAL_program_name:
23429 case OPT_SPECIAL_input_file:
23430 case OPT_grecord_gcc_switches:
23431 case OPT_gno_record_gcc_switches:
23432 case OPT__output_pch_:
23433 case OPT_fdiagnostics_show_location_:
23434 case OPT_fdiagnostics_show_option:
23435 case OPT_fdiagnostics_show_caret:
23436 case OPT_fdiagnostics_color_:
23437 case OPT_fverbose_asm:
23438 case OPT____:
23439 case OPT__sysroot_:
23440 case OPT_nostdinc:
23441 case OPT_nostdinc__:
23442 case OPT_fpreprocessed:
23443 case OPT_fltrans_output_list_:
23444 case OPT_fresolution_:
23445 case OPT_fdebug_prefix_map_:
23446 /* Ignore these. */
23447 continue;
23448 default:
23449 if (cl_options[save_decoded_options[j].opt_index].flags
23450 & CL_NO_DWARF_RECORD)
23451 continue;
23452 gcc_checking_assert (save_decoded_options[j].canonical_option[0][0]
23453 == '-');
23454 switch (save_decoded_options[j].canonical_option[0][1])
23455 {
23456 case 'M':
23457 case 'i':
23458 case 'W':
23459 continue;
23460 case 'f':
23461 if (strncmp (save_decoded_options[j].canonical_option[0] + 2,
23462 "dump", 4) == 0)
23463 continue;
23464 break;
23465 default:
23466 break;
23467 }
23468 switches.safe_push (save_decoded_options[j].orig_option_with_args_text);
23469 len += strlen (save_decoded_options[j].orig_option_with_args_text) + 1;
23470 break;
23471 }
23472
23473 producer = XNEWVEC (char, plen + 1 + len + 1);
23474 tail = producer;
23475 sprintf (tail, "%s %s", language_string, version_string);
23476 tail += plen;
23477
23478 FOR_EACH_VEC_ELT (switches, j, p)
23479 {
23480 len = strlen (p);
23481 *tail = ' ';
23482 memcpy (tail + 1, p, len);
23483 tail += len + 1;
23484 }
23485
23486 *tail = '\0';
23487 return producer;
23488}
23489
23490/* Given a C and/or C++ language/version string return the "highest".
23491 C++ is assumed to be "higher" than C in this case. Used for merging
23492 LTO translation unit languages. */
23493static const char *
23494highest_c_language (const char *lang1, const char *lang2)
23495{
23496 if (strcmp ("GNU C++14", lang1) == 0 || strcmp ("GNU C++14", lang2) == 0)
23497 return "GNU C++14";
23498 if (strcmp ("GNU C++11", lang1) == 0 || strcmp ("GNU C++11", lang2) == 0)
23499 return "GNU C++11";
23500 if (strcmp ("GNU C++98", lang1) == 0 || strcmp ("GNU C++98", lang2) == 0)
23501 return "GNU C++98";
23502
23503 if (strcmp ("GNU C11", lang1) == 0 || strcmp ("GNU C11", lang2) == 0)
23504 return "GNU C11";
23505 if (strcmp ("GNU C99", lang1) == 0 || strcmp ("GNU C99", lang2) == 0)
23506 return "GNU C99";
23507 if (strcmp ("GNU C89", lang1) == 0 || strcmp ("GNU C89", lang2) == 0)
23508 return "GNU C89";
23509
23510 gcc_unreachable ();
23511}
23512
23513
23514/* Generate the DIE for the compilation unit. */
23515
23516static dw_die_ref
23517gen_compile_unit_die (const char *filename)
23518{
23519 dw_die_ref die;
23520 const char *language_string = lang_hooks.name;
23521 int language;
23522
23523 die = new_die (DW_TAG_compile_unit, NULL, NULL);
23524
23525 if (filename)
23526 {
23527 add_name_attribute (die, filename);
23528 /* Don't add cwd for <built-in>. */
23529 if (filename[0] != '<')
23530 add_comp_dir_attribute (die);
23531 }
23532
23533 add_AT_string (die, DW_AT_producer, producer_string ? producer_string : "");
23534
23535 /* If our producer is LTO try to figure out a common language to use
23536 from the global list of translation units. */
23537 if (strcmp (language_string, "GNU GIMPLE") == 0)
23538 {
23539 unsigned i;
23540 tree t;
23541 const char *common_lang = NULL;
23542
23543 FOR_EACH_VEC_SAFE_ELT (all_translation_units, i, t)
23544 {
23545 if (!TRANSLATION_UNIT_LANGUAGE (t))
23546 continue;
23547 if (!common_lang)
23548 common_lang = TRANSLATION_UNIT_LANGUAGE (t);
23549 else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
23550 ;
23551 else if (strncmp (common_lang, "GNU C", 5) == 0
23552 && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
23553 /* Mixing C and C++ is ok, use C++ in that case. */
23554 common_lang = highest_c_language (common_lang,
23555 TRANSLATION_UNIT_LANGUAGE (t));
23556 else
23557 {
23558 /* Fall back to C. */
23559 common_lang = NULL;
23560 break;
23561 }
23562 }
23563
23564 if (common_lang)
23565 language_string = common_lang;
23566 }
23567
23568 language = DW_LANG_C;
23569 if (strncmp (language_string, "GNU C", 5) == 0
23570 && ISDIGIT (language_string[5]))
23571 {
23572 language = DW_LANG_C89;
23573 if (dwarf_version >= 3 || !dwarf_strict)
23574 {
23575 if (strcmp (language_string, "GNU C89") != 0)
23576 language = DW_LANG_C99;
23577
23578 if (dwarf_version >= 5 /* || !dwarf_strict */)
23579 if (strcmp (language_string, "GNU C11") == 0)
23580 language = DW_LANG_C11;
23581 }
23582 }
23583 else if (strncmp (language_string, "GNU C++", 7) == 0)
23584 {
23585 language = DW_LANG_C_plus_plus;
23586 if (dwarf_version >= 5 /* || !dwarf_strict */)
23587 {
23588 if (strcmp (language_string, "GNU C++11") == 0)
23589 language = DW_LANG_C_plus_plus_11;
23590 else if (strcmp (language_string, "GNU C++14") == 0)
23591 language = DW_LANG_C_plus_plus_14;
23592 }
23593 }
23594 else if (strcmp (language_string, "GNU F77") == 0)
23595 language = DW_LANG_Fortran77;
23596 else if (strcmp (language_string, "GNU Pascal") == 0)
23597 language = DW_LANG_Pascal83;
23598 else if (dwarf_version >= 3 || !dwarf_strict)
23599 {
23600 if (strcmp (language_string, "GNU Ada") == 0)
23601 language = DW_LANG_Ada95;
23602 else if (strncmp (language_string, "GNU Fortran", 11) == 0)
23603 {
23604 language = DW_LANG_Fortran95;
23605 if (dwarf_version >= 5 /* || !dwarf_strict */)
23606 {
23607 if (strcmp (language_string, "GNU Fortran2003") == 0)
23608 language = DW_LANG_Fortran03;
23609 else if (strcmp (language_string, "GNU Fortran2008") == 0)
23610 language = DW_LANG_Fortran08;
23611 }
23612 }
23613 else if (strcmp (language_string, "GNU Java") == 0)
23614 language = DW_LANG_Java;
23615 else if (strcmp (language_string, "GNU Objective-C") == 0)
23616 language = DW_LANG_ObjC;
23617 else if (strcmp (language_string, "GNU Objective-C++") == 0)
23618 language = DW_LANG_ObjC_plus_plus;
23619 else if (dwarf_version >= 5 || !dwarf_strict)
23620 {
23621 if (strcmp (language_string, "GNU Go") == 0)
23622 language = DW_LANG_Go;
23623 }
23624 }
23625 /* Use a degraded Fortran setting in strict DWARF2 so is_fortran works. */
23626 else if (strncmp (language_string, "GNU Fortran", 11) == 0)
23627 language = DW_LANG_Fortran90;
23628
23629 add_AT_unsigned (die, DW_AT_language, language);
23630
23631 switch (language)
23632 {
23633 case DW_LANG_Fortran77:
23634 case DW_LANG_Fortran90:
23635 case DW_LANG_Fortran95:
23636 case DW_LANG_Fortran03:
23637 case DW_LANG_Fortran08:
23638 /* Fortran has case insensitive identifiers and the front-end
23639 lowercases everything. */
23640 add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
23641 break;
23642 default:
23643 /* The default DW_ID_case_sensitive doesn't need to be specified. */
23644 break;
23645 }
23646 return die;
23647}
23648
23649/* Generate the DIE for a base class. */
23650
23651static void
23652gen_inheritance_die (tree binfo, tree access, tree type,
23653 dw_die_ref context_die)
23654{
23655 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
23656 struct vlr_context ctx = { type, NULL };
23657
23658 add_type_attribute (die, BINFO_TYPE (binfo), TYPE_UNQUALIFIED, false,
23659 context_die);
23660 add_data_member_location_attribute (die, binfo, &ctx);
23661
23662 if (BINFO_VIRTUAL_P (binfo))
23663 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
23664
23665 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
23666 children, otherwise the default is DW_ACCESS_public. In DWARF2
23667 the default has always been DW_ACCESS_private. */
23668 if (access == access_public_node)
23669 {
23670 if (dwarf_version == 2
23671 || context_die->die_tag == DW_TAG_class_type)
23672 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
23673 }
23674 else if (access == access_protected_node)
23675 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
23676 else if (dwarf_version > 2
23677 && context_die->die_tag != DW_TAG_class_type)
23678 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
23679}
23680
23681/* Return whether DECL is a FIELD_DECL that represents the variant part of a
23682 structure. */
23683static bool
23684is_variant_part (tree decl)
23685{
23686 return (TREE_CODE (decl) == FIELD_DECL
23687 && TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE);
23688}
23689
23690/* Check that OPERAND is a reference to a field in STRUCT_TYPE. If it is,
23691 return the FIELD_DECL. Return NULL_TREE otherwise. */
23692
23693static tree
23694analyze_discr_in_predicate (tree operand, tree struct_type)
23695{
23696 bool continue_stripping = true;
23697 while (continue_stripping)
23698 switch (TREE_CODE (operand))
23699 {
23700 CASE_CONVERT:
23701 operand = TREE_OPERAND (operand, 0);
23702 break;
23703 default:
23704 continue_stripping = false;
23705 break;
23706 }
23707
23708 /* Match field access to members of struct_type only. */
23709 if (TREE_CODE (operand) == COMPONENT_REF
23710 && TREE_CODE (TREE_OPERAND (operand, 0)) == PLACEHOLDER_EXPR
23711 && TREE_TYPE (TREE_OPERAND (operand, 0)) == struct_type
23712 && TREE_CODE (TREE_OPERAND (operand, 1)) == FIELD_DECL)
23713 return TREE_OPERAND (operand, 1);
23714 else
23715 return NULL_TREE;
23716}
23717
23718/* Check that SRC is a constant integer that can be represented as a native
23719 integer constant (either signed or unsigned). If so, store it into DEST and
23720 return true. Return false otherwise. */
23721
23722static bool
23723get_discr_value (tree src, dw_discr_value *dest)
23724{
23725 bool is_unsigned = TYPE_UNSIGNED (TREE_TYPE (src));
23726
23727 if (TREE_CODE (src) != INTEGER_CST
23728 || !(is_unsigned ? tree_fits_uhwi_p (src) : tree_fits_shwi_p (src)))
23729 return false;
23730
23731 dest->pos = is_unsigned;
23732 if (is_unsigned)
23733 dest->v.uval = tree_to_uhwi (src);
23734 else
23735 dest->v.sval = tree_to_shwi (src);
23736
23737 return true;
23738}
23739
23740/* Try to extract synthetic properties out of VARIANT_PART_DECL, which is a
23741 FIELD_DECL in STRUCT_TYPE that represents a variant part. If unsuccessful,
23742 store NULL_TREE in DISCR_DECL. Otherwise:
23743
23744 - store the discriminant field in STRUCT_TYPE that controls the variant
23745 part to *DISCR_DECL
23746
23747 - put in *DISCR_LISTS_P an array where for each variant, the item
23748 represents the corresponding matching list of discriminant values.
23749
23750 - put in *DISCR_LISTS_LENGTH the number of variants, which is the size of
23751 the above array.
23752
23753 Note that when the array is allocated (i.e. when the analysis is
23754 successful), it is up to the caller to free the array. */
23755
23756static void
23757analyze_variants_discr (tree variant_part_decl,
23758 tree struct_type,
23759 tree *discr_decl,
23760 dw_discr_list_ref **discr_lists_p,
23761 unsigned *discr_lists_length)
23762{
23763 tree variant_part_type = TREE_TYPE (variant_part_decl);
23764 tree variant;
23765 dw_discr_list_ref *discr_lists;
23766 unsigned i;
23767
23768 /* Compute how many variants there are in this variant part. */
23769 *discr_lists_length = 0;
23770 for (variant = TYPE_FIELDS (variant_part_type);
23771 variant != NULL_TREE;
23772 variant = DECL_CHAIN (variant))
23773 ++*discr_lists_length;
23774
23775 *discr_decl = NULL_TREE;
23776 *discr_lists_p
23777 = (dw_discr_list_ref *) xcalloc (*discr_lists_length,
23778 sizeof (**discr_lists_p));
23779 discr_lists = *discr_lists_p;
23780
23781 /* And then analyze all variants to extract discriminant information for all
23782 of them. This analysis is conservative: as soon as we detect something we
23783 do not support, abort everything and pretend we found nothing. */
23784 for (variant = TYPE_FIELDS (variant_part_type), i = 0;
23785 variant != NULL_TREE;
23786 variant = DECL_CHAIN (variant), ++i)
23787 {
23788 tree match_expr = DECL_QUALIFIER (variant);
23789
23790 /* Now, try to analyze the predicate and deduce a discriminant for
23791 it. */
23792 if (match_expr == boolean_true_node)
23793 /* Typically happens for the default variant: it matches all cases that
23794 previous variants rejected. Don't output any matching value for
23795 this one. */
23796 continue;
23797
23798 /* The following loop tries to iterate over each discriminant
23799 possibility: single values or ranges. */
23800 while (match_expr != NULL_TREE)
23801 {
23802 tree next_round_match_expr;
23803 tree candidate_discr = NULL_TREE;
23804 dw_discr_list_ref new_node = NULL;
23805
23806 /* Possibilities are matched one after the other by nested
23807 TRUTH_ORIF_EXPR expressions. Process the current possibility and
23808 continue with the rest at next iteration. */
23809 if (TREE_CODE (match_expr) == TRUTH_ORIF_EXPR)
23810 {
23811 next_round_match_expr = TREE_OPERAND (match_expr, 0);
23812 match_expr = TREE_OPERAND (match_expr, 1);
23813 }
23814 else
23815 next_round_match_expr = NULL_TREE;
23816
23817 if (match_expr == boolean_false_node)
23818 /* This sub-expression matches nothing: just wait for the next
23819 one. */
23820 ;
23821
23822 else if (TREE_CODE (match_expr) == EQ_EXPR)
23823 {
23824 /* We are matching: <discr_field> == <integer_cst>
23825 This sub-expression matches a single value. */
23826 tree integer_cst = TREE_OPERAND (match_expr, 1);
23827
23828 candidate_discr
23829 = analyze_discr_in_predicate (TREE_OPERAND (match_expr, 0),
23830 struct_type);
23831
23832 new_node = ggc_cleared_alloc<dw_discr_list_node> ();
23833 if (!get_discr_value (integer_cst,
23834 &new_node->dw_discr_lower_bound))
23835 goto abort;
23836 new_node->dw_discr_range = false;
23837 }
23838
23839 else if (TREE_CODE (match_expr) == TRUTH_ANDIF_EXPR)
23840 {
23841 /* We are matching:
23842 <discr_field> > <integer_cst>
23843 && <discr_field> < <integer_cst>.
23844 This sub-expression matches the range of values between the
23845 two matched integer constants. Note that comparisons can be
23846 inclusive or exclusive. */
23847 tree candidate_discr_1, candidate_discr_2;
23848 tree lower_cst, upper_cst;
23849 bool lower_cst_included, upper_cst_included;
23850 tree lower_op = TREE_OPERAND (match_expr, 0);
23851 tree upper_op = TREE_OPERAND (match_expr, 1);
23852
23853 /* When the comparison is exclusive, the integer constant is not
23854 the discriminant range bound we are looking for: we will have
23855 to increment or decrement it. */
23856 if (TREE_CODE (lower_op) == GE_EXPR)
23857 lower_cst_included = true;
23858 else if (TREE_CODE (lower_op) == GT_EXPR)
23859 lower_cst_included = false;
23860 else
23861 goto abort;
23862
23863 if (TREE_CODE (upper_op) == LE_EXPR)
23864 upper_cst_included = true;
23865 else if (TREE_CODE (upper_op) == LT_EXPR)
23866 upper_cst_included = false;
23867 else
23868 goto abort;
23869
23870 /* Extract the discriminant from the first operand and check it
23871 is consistant with the same analysis in the second
23872 operand. */
23873 candidate_discr_1
23874 = analyze_discr_in_predicate (TREE_OPERAND (lower_op, 0),
23875 struct_type);
23876 candidate_discr_2
23877 = analyze_discr_in_predicate (TREE_OPERAND (upper_op, 0),
23878 struct_type);
23879 if (candidate_discr_1 == candidate_discr_2)
23880 candidate_discr = candidate_discr_1;
23881 else
23882 goto abort;
23883
23884 /* Extract bounds from both. */
23885 new_node = ggc_cleared_alloc<dw_discr_list_node> ();
23886 lower_cst = TREE_OPERAND (lower_op, 1);
23887 upper_cst = TREE_OPERAND (upper_op, 1);
23888
23889 if (!lower_cst_included)
23890 lower_cst
23891 = fold_build2 (PLUS_EXPR, TREE_TYPE (lower_cst), lower_cst,
23892 build_int_cst (TREE_TYPE (lower_cst), 1));
23893 if (!upper_cst_included)
23894 upper_cst
23895 = fold_build2 (MINUS_EXPR, TREE_TYPE (upper_cst), upper_cst,
23896 build_int_cst (TREE_TYPE (upper_cst), 1));
23897
23898 if (!get_discr_value (lower_cst,
23899 &new_node->dw_discr_lower_bound)
23900 || !get_discr_value (upper_cst,
23901 &new_node->dw_discr_upper_bound))
23902 goto abort;
23903
23904 new_node->dw_discr_range = true;
23905 }
23906
23907 else
23908 /* Unsupported sub-expression: we cannot determine the set of
23909 matching discriminant values. Abort everything. */
23910 goto abort;
23911
23912 /* If the discriminant info is not consistant with what we saw so
23913 far, consider the analysis failed and abort everything. */
23914 if (candidate_discr == NULL_TREE
23915 || (*discr_decl != NULL_TREE && candidate_discr != *discr_decl))
23916 goto abort;
23917 else
23918 *discr_decl = candidate_discr;
23919
23920 if (new_node != NULL)
23921 {
23922 new_node->dw_discr_next = discr_lists[i];
23923 discr_lists[i] = new_node;
23924 }
23925 match_expr = next_round_match_expr;
23926 }
23927 }
23928
23929 /* If we reach this point, we could match everything we were interested
23930 in. */
23931 return;
23932
23933abort:
23934 /* Clean all data structure and return no result. */
23935 free (*discr_lists_p);
23936 *discr_lists_p = NULL;
23937 *discr_decl = NULL_TREE;
23938}
23939
23940/* Generate a DIE to represent VARIANT_PART_DECL, a variant part that is part
23941 of STRUCT_TYPE, a record type. This new DIE is emitted as the next child
23942 under CONTEXT_DIE.
23943
23944 Variant parts are supposed to be implemented as a FIELD_DECL whose type is a
23945 QUAL_UNION_TYPE: this is the VARIANT_PART_DECL parameter. The members for
23946 this type, which are record types, represent the available variants and each
23947 has a DECL_QUALIFIER attribute. The discriminant and the discriminant
23948 values are inferred from these attributes.
23949
23950 In trees, the offsets for the fields inside these sub-records are relative
23951 to the variant part itself, whereas the corresponding DIEs should have
23952 offset attributes that are relative to the embedding record base address.
23953 This is why the caller must provide a VARIANT_PART_OFFSET expression: it
23954 must be an expression that computes the offset of the variant part to
23955 describe in DWARF. */
23956
23957static void
23958gen_variant_part (tree variant_part_decl, struct vlr_context *vlr_ctx,
23959 dw_die_ref context_die)
23960{
23961 const tree variant_part_type = TREE_TYPE (variant_part_decl);
23962 tree variant_part_offset = vlr_ctx->variant_part_offset;
23963 struct loc_descr_context ctx = {
23964 vlr_ctx->struct_type, /* context_type */
23965 NULL_TREE, /* base_decl */
23966 NULL, /* dpi */
23967 false, /* placeholder_arg */
23968 false /* placeholder_seen */
23969 };
23970
23971 /* The FIELD_DECL node in STRUCT_TYPE that acts as the discriminant, or
23972 NULL_TREE if there is no such field. */
23973 tree discr_decl = NULL_TREE;
23974 dw_discr_list_ref *discr_lists;
23975 unsigned discr_lists_length = 0;
23976 unsigned i;
23977
23978 dw_die_ref dwarf_proc_die = NULL;
23979 dw_die_ref variant_part_die
23980 = new_die (DW_TAG_variant_part, context_die, variant_part_type);
23981
23982 equate_decl_number_to_die (variant_part_decl, variant_part_die);
23983
23984 analyze_variants_discr (variant_part_decl, vlr_ctx->struct_type,
23985 &discr_decl, &discr_lists, &discr_lists_length);
23986
23987 if (discr_decl != NULL_TREE)
23988 {
23989 dw_die_ref discr_die = lookup_decl_die (discr_decl);
23990
23991 if (discr_die)
23992 add_AT_die_ref (variant_part_die, DW_AT_discr, discr_die);
23993 else
23994 /* We have no DIE for the discriminant, so just discard all
23995 discrimimant information in the output. */
23996 discr_decl = NULL_TREE;
23997 }
23998
23999 /* If the offset for this variant part is more complex than a constant,
24000 create a DWARF procedure for it so that we will not have to generate DWARF
24001 expressions for it for each member. */
24002 if (TREE_CODE (variant_part_offset) != INTEGER_CST
24003 && (dwarf_version >= 3 || !dwarf_strict))
24004 {
24005 const tree dwarf_proc_fndecl
24006 = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL, NULL_TREE,
24007 build_function_type (TREE_TYPE (variant_part_offset),
24008 NULL_TREE));
24009 const tree dwarf_proc_call = build_call_expr (dwarf_proc_fndecl, 0);
24010 const dw_loc_descr_ref dwarf_proc_body
24011 = loc_descriptor_from_tree (variant_part_offset, 0, &ctx);
24012
24013 dwarf_proc_die = new_dwarf_proc_die (dwarf_proc_body,
24014 dwarf_proc_fndecl, context_die);
24015 if (dwarf_proc_die != NULL)
24016 variant_part_offset = dwarf_proc_call;
24017 }
24018
24019 /* Output DIEs for all variants. */
24020 i = 0;
24021 for (tree variant = TYPE_FIELDS (variant_part_type);
24022 variant != NULL_TREE;
24023 variant = DECL_CHAIN (variant), ++i)
24024 {
24025 tree variant_type = TREE_TYPE (variant);
24026 dw_die_ref variant_die;
24027
24028 /* All variants (i.e. members of a variant part) are supposed to be
24029 encoded as structures. Sub-variant parts are QUAL_UNION_TYPE fields
24030 under these records. */
24031 gcc_assert (TREE_CODE (variant_type) == RECORD_TYPE);
24032
24033 variant_die = new_die (DW_TAG_variant, variant_part_die, variant_type);
24034 equate_decl_number_to_die (variant, variant_die);
24035
24036 /* Output discriminant values this variant matches, if any. */
24037 if (discr_decl == NULL || discr_lists[i] == NULL)
24038 /* In the case we have discriminant information at all, this is
24039 probably the default variant: as the standard says, don't
24040 output any discriminant value/list attribute. */
24041 ;
24042 else if (discr_lists[i]->dw_discr_next == NULL
24043 && !discr_lists[i]->dw_discr_range)
24044 /* If there is only one accepted value, don't bother outputting a
24045 list. */
24046 add_discr_value (variant_die, &discr_lists[i]->dw_discr_lower_bound);
24047 else
24048 add_discr_list (variant_die, discr_lists[i]);
24049
24050 for (tree member = TYPE_FIELDS (variant_type);
24051 member != NULL_TREE;
24052 member = DECL_CHAIN (member))
24053 {
24054 struct vlr_context vlr_sub_ctx = {
24055 vlr_ctx->struct_type, /* struct_type */
24056 NULL /* variant_part_offset */
24057 };
24058 if (is_variant_part (member))
24059 {
24060 /* All offsets for fields inside variant parts are relative to
24061 the top-level embedding RECORD_TYPE's base address. On the
24062 other hand, offsets in GCC's types are relative to the
24063 nested-most variant part. So we have to sum offsets each time
24064 we recurse. */
24065
24066 vlr_sub_ctx.variant_part_offset
24067 = fold_build2 (PLUS_EXPR, TREE_TYPE (variant_part_offset),
24068 variant_part_offset, byte_position (member));
24069 gen_variant_part (member, &vlr_sub_ctx, variant_die);
24070 }
24071 else
24072 {
24073 vlr_sub_ctx.variant_part_offset = variant_part_offset;
24074 gen_decl_die (member, NULL, &vlr_sub_ctx, variant_die);
24075 }
24076 }
24077 }
24078
24079 free (discr_lists);
24080}
24081
24082/* Generate a DIE for a class member. */
24083
24084static void
24085gen_member_die (tree type, dw_die_ref context_die)
24086{
24087 tree member;
24088 tree binfo = TYPE_BINFO (type);
24089 dw_die_ref child;
24090
24091 /* If this is not an incomplete type, output descriptions of each of its
24092 members. Note that as we output the DIEs necessary to represent the
24093 members of this record or union type, we will also be trying to output
24094 DIEs to represent the *types* of those members. However the `type'
24095 function (above) will specifically avoid generating type DIEs for member
24096 types *within* the list of member DIEs for this (containing) type except
24097 for those types (of members) which are explicitly marked as also being
24098 members of this (containing) type themselves. The g++ front- end can
24099 force any given type to be treated as a member of some other (containing)
24100 type by setting the TYPE_CONTEXT of the given (member) type to point to
24101 the TREE node representing the appropriate (containing) type. */
24102
24103 /* First output info about the base classes. */
24104 if (binfo)
24105 {
24106 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
24107 int i;
24108 tree base;
24109
24110 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
24111 gen_inheritance_die (base,
24112 (accesses ? (*accesses)[i] : access_public_node),
24113 type,
24114 context_die);
24115 }
24116
24117 /* Now output info about the data members and type members. */
24118 for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
24119 {
24120 struct vlr_context vlr_ctx = { type, NULL_TREE };
24121 bool static_inline_p
24122 = (TREE_STATIC (member)
24123 && (lang_hooks.decls.decl_dwarf_attribute (member, DW_AT_inline)
24124 != -1));
24125
24126 /* If we thought we were generating minimal debug info for TYPE
24127 and then changed our minds, some of the member declarations
24128 may have already been defined. Don't define them again, but
24129 do put them in the right order. */
24130
24131 child = lookup_decl_die (member);
24132 if (child)
24133 {
24134 /* Handle inline static data members, which only have in-class
24135 declarations. */
24136 dw_die_ref ref = NULL;
24137 if (child->die_tag == DW_TAG_variable
24138 && child->die_parent == comp_unit_die ())
24139 {
24140 ref = get_AT_ref (child, DW_AT_specification);
24141 /* For C++17 inline static data members followed by redundant
24142 out of class redeclaration, we might get here with
24143 child being the DIE created for the out of class
24144 redeclaration and with its DW_AT_specification being
24145 the DIE created for in-class definition. We want to
24146 reparent the latter, and don't want to create another
24147 DIE with DW_AT_specification in that case, because
24148 we already have one. */
24149 if (ref
24150 && static_inline_p
24151 && ref->die_tag == DW_TAG_variable
24152 && ref->die_parent == comp_unit_die ()
24153 && get_AT (ref, DW_AT_specification) == NULL)
24154 {
24155 child = ref;
24156 ref = NULL;
24157 static_inline_p = false;
24158 }
24159 }
24160 if (child->die_tag == DW_TAG_variable
24161 && child->die_parent == comp_unit_die ()
24162 && ref == NULL)
24163 {
24164 reparent_child (child, context_die);
24165 if (dwarf_version < 5)
24166 child->die_tag = DW_TAG_member;
24167 }
24168 else
24169 splice_child_die (context_die, child);
24170 }
24171
24172 /* Do not generate standard DWARF for variant parts if we are generating
24173 the corresponding GNAT encodings: DIEs generated for both would
24174 conflict in our mappings. */
24175 else if (is_variant_part (member)
24176 && gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL)
24177 {
24178 vlr_ctx.variant_part_offset = byte_position (member);
24179 gen_variant_part (member, &vlr_ctx, context_die);
24180 }
24181 else
24182 {
24183 vlr_ctx.variant_part_offset = NULL_TREE;
24184 gen_decl_die (member, NULL, &vlr_ctx, context_die);
24185 }
24186
24187 /* For C++ inline static data members emit immediately a DW_TAG_variable
24188 DIE that will refer to that DW_TAG_member/DW_TAG_variable through
24189 DW_AT_specification. */
24190 if (static_inline_p)
24191 {
24192 int old_extern = DECL_EXTERNAL (member);
24193 DECL_EXTERNAL (member) = 0;
24194 gen_decl_die (member, NULL, NULL, comp_unit_die ());
24195 DECL_EXTERNAL (member) = old_extern;
24196 }
24197 }
24198
24199 /* We do not keep type methods in type variants. */
24200 gcc_assert (TYPE_MAIN_VARIANT (type) == type);
24201 /* Now output info about the function members (if any). */
24202 if (TYPE_METHODS (type) != error_mark_node)
24203 for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
24204 {
24205 /* Don't include clones in the member list. */
24206 if (DECL_ABSTRACT_ORIGIN (member))
24207 continue;
24208 /* Nor constructors for anonymous classes. */
24209 if (DECL_ARTIFICIAL (member)
24210 && dwarf2_name (member, 0) == NULL)
24211 continue;
24212
24213 child = lookup_decl_die (member);
24214 if (child)
24215 splice_child_die (context_die, child);
24216 else
24217 gen_decl_die (member, NULL, NULL, context_die);
24218 }
24219}
24220
24221/* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
24222 is set, we pretend that the type was never defined, so we only get the
24223 member DIEs needed by later specification DIEs. */
24224
24225static void
24226gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
24227 enum debug_info_usage usage)
24228{
24229 if (TREE_ASM_WRITTEN (type))
24230 {
24231 /* Fill in the bound of variable-length fields in late dwarf if
24232 still incomplete. */
24233 if (!early_dwarf && variably_modified_type_p (type, NULL))
24234 for (tree member = TYPE_FIELDS (type);
24235 member;
24236 member = DECL_CHAIN (member))
24237 fill_variable_array_bounds (TREE_TYPE (member));
24238 return;
24239 }
24240
24241 dw_die_ref type_die = lookup_type_die (type);
24242 dw_die_ref scope_die = 0;
24243 int nested = 0;
24244 int complete = (TYPE_SIZE (type)
24245 && (! TYPE_STUB_DECL (type)
24246 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
24247 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
24248 complete = complete && should_emit_struct_debug (type, usage);
24249
24250 if (type_die && ! complete)
24251 return;
24252
24253 if (TYPE_CONTEXT (type) != NULL_TREE
24254 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
24255 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
24256 nested = 1;
24257
24258 scope_die = scope_die_for (type, context_die);
24259
24260 /* Generate child dies for template paramaters. */
24261 if (!type_die && debug_info_level > DINFO_LEVEL_TERSE)
24262 schedule_generic_params_dies_gen (type);
24263
24264 if (! type_die || (nested && is_cu_die (scope_die)))
24265 /* First occurrence of type or toplevel definition of nested class. */
24266 {
24267 dw_die_ref old_die = type_die;
24268
24269 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
24270 ? record_type_tag (type) : DW_TAG_union_type,
24271 scope_die, type);
24272 equate_type_number_to_die (type, type_die);
24273 if (old_die)
24274 add_AT_specification (type_die, old_die);
24275 else
24276 add_name_attribute (type_die, type_tag (type));
24277 }
24278 else
24279 remove_AT (type_die, DW_AT_declaration);
24280
24281 /* If this type has been completed, then give it a byte_size attribute and
24282 then give a list of members. */
24283 if (complete && !ns_decl)
24284 {
24285 /* Prevent infinite recursion in cases where the type of some member of
24286 this type is expressed in terms of this type itself. */
24287 TREE_ASM_WRITTEN (type) = 1;
24288 add_byte_size_attribute (type_die, type);
24289 add_alignment_attribute (type_die, type);
24290 if (TYPE_STUB_DECL (type) != NULL_TREE)
24291 {
24292 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
24293 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
24294 }
24295
24296 /* If the first reference to this type was as the return type of an
24297 inline function, then it may not have a parent. Fix this now. */
24298 if (type_die->die_parent == NULL)
24299 add_child_die (scope_die, type_die);
24300
24301 push_decl_scope (type);
24302 gen_member_die (type, type_die);
24303 pop_decl_scope ();
24304
24305 add_gnat_descriptive_type_attribute (type_die, type, context_die);
24306 if (TYPE_ARTIFICIAL (type))
24307 add_AT_flag (type_die, DW_AT_artificial, 1);
24308
24309 /* GNU extension: Record what type our vtable lives in. */
24310 if (TYPE_VFIELD (type))
24311 {
24312 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
24313
24314 gen_type_die (vtype, context_die);
24315 add_AT_die_ref (type_die, DW_AT_containing_type,
24316 lookup_type_die (vtype));
24317 }
24318 }
24319 else
24320 {
24321 add_AT_flag (type_die, DW_AT_declaration, 1);
24322
24323 /* We don't need to do this for function-local types. */
24324 if (TYPE_STUB_DECL (type)
24325 && ! decl_function_context (TYPE_STUB_DECL (type)))
24326 vec_safe_push (incomplete_types, type);
24327 }
24328
24329 if (get_AT (type_die, DW_AT_name))
24330 add_pubtype (type, type_die);
24331}
24332
24333/* Generate a DIE for a subroutine _type_. */
24334
24335static void
24336gen_subroutine_type_die (tree type, dw_die_ref context_die)
24337{
24338 tree return_type = TREE_TYPE (type);
24339 dw_die_ref subr_die
24340 = new_die (DW_TAG_subroutine_type,
24341 scope_die_for (type, context_die), type);
24342
24343 equate_type_number_to_die (type, subr_die);
24344 add_prototyped_attribute (subr_die, type);
24345 add_type_attribute (subr_die, return_type, TYPE_UNQUALIFIED, false,
24346 context_die);
24347 add_alignment_attribute (subr_die, type);
24348 gen_formal_types_die (type, subr_die);
24349
24350 if (get_AT (subr_die, DW_AT_name))
24351 add_pubtype (type, subr_die);
24352 if ((dwarf_version >= 5 || !dwarf_strict)
24353 && lang_hooks.types.type_dwarf_attribute (type, DW_AT_reference) != -1)
24354 add_AT_flag (subr_die, DW_AT_reference, 1);
24355 if ((dwarf_version >= 5 || !dwarf_strict)
24356 && lang_hooks.types.type_dwarf_attribute (type,
24357 DW_AT_rvalue_reference) != -1)
24358 add_AT_flag (subr_die, DW_AT_rvalue_reference, 1);
24359}
24360
24361/* Generate a DIE for a type definition. */
24362
24363static void
24364gen_typedef_die (tree decl, dw_die_ref context_die)
24365{
24366 dw_die_ref type_die;
24367 tree type;
24368
24369 if (TREE_ASM_WRITTEN (decl))
24370 {
24371 if (DECL_ORIGINAL_TYPE (decl))
24372 fill_variable_array_bounds (DECL_ORIGINAL_TYPE (decl));
24373 return;
24374 }
24375
24376 /* As we avoid creating DIEs for local typedefs (see decl_ultimate_origin
24377 checks in process_scope_var and modified_type_die), this should be called
24378 only for original types. */
24379 gcc_assert (decl_ultimate_origin (decl) == NULL);
24380
24381 TREE_ASM_WRITTEN (decl) = 1;
24382 type_die = new_die (DW_TAG_typedef, context_die, decl);
24383
24384 add_name_and_src_coords_attributes (type_die, decl);
24385 if (DECL_ORIGINAL_TYPE (decl))
24386 {
24387 type = DECL_ORIGINAL_TYPE (decl);
24388 if (type == error_mark_node)
24389 return;
24390
24391 gcc_assert (type != TREE_TYPE (decl));
24392 equate_type_number_to_die (TREE_TYPE (decl), type_die);
24393 }
24394 else
24395 {
24396 type = TREE_TYPE (decl);
24397 if (type == error_mark_node)
24398 return;
24399
24400 if (is_naming_typedef_decl (TYPE_NAME (type)))
24401 {
24402 /* Here, we are in the case of decl being a typedef naming
24403 an anonymous type, e.g:
24404 typedef struct {...} foo;
24405 In that case TREE_TYPE (decl) is not a typedef variant
24406 type and TYPE_NAME of the anonymous type is set to the
24407 TYPE_DECL of the typedef. This construct is emitted by
24408 the C++ FE.
24409
24410 TYPE is the anonymous struct named by the typedef
24411 DECL. As we need the DW_AT_type attribute of the
24412 DW_TAG_typedef to point to the DIE of TYPE, let's
24413 generate that DIE right away. add_type_attribute
24414 called below will then pick (via lookup_type_die) that
24415 anonymous struct DIE. */
24416 if (!TREE_ASM_WRITTEN (type))
24417 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
24418
24419 /* This is a GNU Extension. We are adding a
24420 DW_AT_linkage_name attribute to the DIE of the
24421 anonymous struct TYPE. The value of that attribute
24422 is the name of the typedef decl naming the anonymous
24423 struct. This greatly eases the work of consumers of
24424 this debug info. */
24425 add_linkage_name_raw (lookup_type_die (type), decl);
24426 }
24427 }
24428
24429 add_type_attribute (type_die, type, decl_quals (decl), false,
24430 context_die);
24431
24432 if (is_naming_typedef_decl (decl))
24433 /* We want that all subsequent calls to lookup_type_die with
24434 TYPE in argument yield the DW_TAG_typedef we have just
24435 created. */
24436 equate_type_number_to_die (type, type_die);
24437
24438 add_alignment_attribute (type_die, TREE_TYPE (decl));
24439
24440 add_accessibility_attribute (type_die, decl);
24441
24442 if (DECL_ABSTRACT_P (decl))
24443 equate_decl_number_to_die (decl, type_die);
24444
24445 if (get_AT (type_die, DW_AT_name))
24446 add_pubtype (decl, type_die);
24447}
24448
24449/* Generate a DIE for a struct, class, enum or union type. */
24450
24451static void
24452gen_tagged_type_die (tree type,
24453 dw_die_ref context_die,
24454 enum debug_info_usage usage)
24455{
24456 int need_pop;
24457
24458 if (type == NULL_TREE
24459 || !is_tagged_type (type))
24460 return;
24461
24462 if (TREE_ASM_WRITTEN (type))
24463 need_pop = 0;
24464 /* If this is a nested type whose containing class hasn't been written
24465 out yet, writing it out will cover this one, too. This does not apply
24466 to instantiations of member class templates; they need to be added to
24467 the containing class as they are generated. FIXME: This hurts the
24468 idea of combining type decls from multiple TUs, since we can't predict
24469 what set of template instantiations we'll get. */
24470 else if (TYPE_CONTEXT (type)
24471 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
24472 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
24473 {
24474 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
24475
24476 if (TREE_ASM_WRITTEN (type))
24477 return;
24478
24479 /* If that failed, attach ourselves to the stub. */
24480 push_decl_scope (TYPE_CONTEXT (type));
24481 context_die = lookup_type_die (TYPE_CONTEXT (type));
24482 need_pop = 1;
24483 }
24484 else if (TYPE_CONTEXT (type) != NULL_TREE
24485 && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
24486 {
24487 /* If this type is local to a function that hasn't been written
24488 out yet, use a NULL context for now; it will be fixed up in
24489 decls_for_scope. */
24490 context_die = lookup_decl_die (TYPE_CONTEXT (type));
24491 /* A declaration DIE doesn't count; nested types need to go in the
24492 specification. */
24493 if (context_die && is_declaration_die (context_die))
24494 context_die = NULL;
24495 need_pop = 0;
24496 }
24497 else
24498 {
24499 context_die = declare_in_namespace (type, context_die);
24500 need_pop = 0;
24501 }
24502
24503 if (TREE_CODE (type) == ENUMERAL_TYPE)
24504 {
24505 /* This might have been written out by the call to
24506 declare_in_namespace. */
24507 if (!TREE_ASM_WRITTEN (type))
24508 gen_enumeration_type_die (type, context_die);
24509 }
24510 else
24511 gen_struct_or_union_type_die (type, context_die, usage);
24512
24513 if (need_pop)
24514 pop_decl_scope ();
24515
24516 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
24517 it up if it is ever completed. gen_*_type_die will set it for us
24518 when appropriate. */
24519}
24520
24521/* Generate a type description DIE. */
24522
24523static void
24524gen_type_die_with_usage (tree type, dw_die_ref context_die,
24525 enum debug_info_usage usage)
24526{
24527 struct array_descr_info info;
24528
24529 if (type == NULL_TREE || type == error_mark_node)
24530 return;
24531
24532 if (flag_checking && type)
24533 verify_type (type);
24534
24535 if (TYPE_NAME (type) != NULL_TREE
24536 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
24537 && is_redundant_typedef (TYPE_NAME (type))
24538 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
24539 /* The DECL of this type is a typedef we don't want to emit debug
24540 info for but we want debug info for its underlying typedef.
24541 This can happen for e.g, the injected-class-name of a C++
24542 type. */
24543 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
24544
24545 /* If TYPE is a typedef type variant, let's generate debug info
24546 for the parent typedef which TYPE is a type of. */
24547 if (typedef_variant_p (type))
24548 {
24549 if (TREE_ASM_WRITTEN (type))
24550 return;
24551
24552 tree name = TYPE_NAME (type);
24553 tree origin = decl_ultimate_origin (name);
24554 if (origin != NULL)
24555 {
24556 gen_decl_die (origin, NULL, NULL, context_die);
24557 return;
24558 }
24559
24560 /* Prevent broken recursion; we can't hand off to the same type. */
24561 gcc_assert (DECL_ORIGINAL_TYPE (name) != type);
24562
24563 /* Give typedefs the right scope. */
24564 context_die = scope_die_for (type, context_die);
24565
24566 TREE_ASM_WRITTEN (type) = 1;
24567
24568 gen_decl_die (name, NULL, NULL, context_die);
24569 return;
24570 }
24571
24572 /* If type is an anonymous tagged type named by a typedef, let's
24573 generate debug info for the typedef. */
24574 if (is_naming_typedef_decl (TYPE_NAME (type)))
24575 {
24576 /* Use the DIE of the containing namespace as the parent DIE of
24577 the type description DIE we want to generate. */
24578 if (DECL_CONTEXT (TYPE_NAME (type))
24579 && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
24580 context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
24581
24582 gen_decl_die (TYPE_NAME (type), NULL, NULL, context_die);
24583 return;
24584 }
24585
24586 if (lang_hooks.types.get_debug_type)
24587 {
24588 tree debug_type = lang_hooks.types.get_debug_type (type);
24589
24590 if (debug_type != NULL_TREE && debug_type != type)
24591 {
24592 gen_type_die_with_usage (debug_type, context_die, usage);
24593 return;
24594 }
24595 }
24596
24597 /* We are going to output a DIE to represent the unqualified version
24598 of this type (i.e. without any const or volatile qualifiers) so
24599 get the main variant (i.e. the unqualified version) of this type
24600 now. (Vectors and arrays are special because the debugging info is in the
24601 cloned type itself. Similarly function/method types can contain extra
24602 ref-qualification). */
24603 if (TREE_CODE (type) == FUNCTION_TYPE
24604 || TREE_CODE (type) == METHOD_TYPE)
24605 {
24606 /* For function/method types, can't use type_main_variant here,
24607 because that can have different ref-qualifiers for C++,
24608 but try to canonicalize. */
24609 tree main = TYPE_MAIN_VARIANT (type);
24610 for (tree t = main; t; t = TYPE_NEXT_VARIANT (t))
24611 if (TYPE_QUALS_NO_ADDR_SPACE (t) == 0
24612 && check_base_type (t, main)
24613 && check_lang_type (t, type))
24614 {
24615 type = t;
24616 break;
24617 }
24618 }
24619 else if (TREE_CODE (type) != VECTOR_TYPE
24620 && TREE_CODE (type) != ARRAY_TYPE)
24621 type = type_main_variant (type);
24622
24623 /* If this is an array type with hidden descriptor, handle it first. */
24624 if (!TREE_ASM_WRITTEN (type)
24625 && lang_hooks.types.get_array_descr_info)
24626 {
24627 memset (&info, 0, sizeof (info));
24628 if (lang_hooks.types.get_array_descr_info (type, &info))
24629 {
24630 /* Fortran sometimes emits array types with no dimension. */
24631 gcc_assert (info.ndimensions >= 0
24632 && (info.ndimensions
24633 <= DWARF2OUT_ARRAY_DESCR_INFO_MAX_DIMEN));
24634 gen_descr_array_type_die (type, &info, context_die);
24635 TREE_ASM_WRITTEN (type) = 1;
24636 return;
24637 }
24638 }
24639
24640 if (TREE_ASM_WRITTEN (type))
24641 {
24642 /* Variable-length types may be incomplete even if
24643 TREE_ASM_WRITTEN. For such types, fall through to
24644 gen_array_type_die() and possibly fill in
24645 DW_AT_{upper,lower}_bound attributes. */
24646 if ((TREE_CODE (type) != ARRAY_TYPE
24647 && TREE_CODE (type) != RECORD_TYPE
24648 && TREE_CODE (type) != UNION_TYPE
24649 && TREE_CODE (type) != QUAL_UNION_TYPE)
24650 || !variably_modified_type_p (type, NULL))
24651 return;
24652 }
24653
24654 switch (TREE_CODE (type))
24655 {
24656 case ERROR_MARK:
24657 break;
24658
24659 case POINTER_TYPE:
24660 case REFERENCE_TYPE:
24661 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
24662 ensures that the gen_type_die recursion will terminate even if the
24663 type is recursive. Recursive types are possible in Ada. */
24664 /* ??? We could perhaps do this for all types before the switch
24665 statement. */
24666 TREE_ASM_WRITTEN (type) = 1;
24667
24668 /* For these types, all that is required is that we output a DIE (or a
24669 set of DIEs) to represent the "basis" type. */
24670 gen_type_die_with_usage (TREE_TYPE (type), context_die,
24671 DINFO_USAGE_IND_USE);
24672 break;
24673
24674 case OFFSET_TYPE:
24675 /* This code is used for C++ pointer-to-data-member types.
24676 Output a description of the relevant class type. */
24677 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
24678 DINFO_USAGE_IND_USE);
24679
24680 /* Output a description of the type of the object pointed to. */
24681 gen_type_die_with_usage (TREE_TYPE (type), context_die,
24682 DINFO_USAGE_IND_USE);
24683
24684 /* Now output a DIE to represent this pointer-to-data-member type
24685 itself. */
24686 gen_ptr_to_mbr_type_die (type, context_die);
24687 break;
24688
24689 case FUNCTION_TYPE:
24690 /* Force out return type (in case it wasn't forced out already). */
24691 gen_type_die_with_usage (TREE_TYPE (type), context_die,
24692 DINFO_USAGE_DIR_USE);
24693 gen_subroutine_type_die (type, context_die);
24694 break;
24695
24696 case METHOD_TYPE:
24697 /* Force out return type (in case it wasn't forced out already). */
24698 gen_type_die_with_usage (TREE_TYPE (type), context_die,
24699 DINFO_USAGE_DIR_USE);
24700 gen_subroutine_type_die (type, context_die);
24701 break;
24702
24703 case ARRAY_TYPE:
24704 case VECTOR_TYPE:
24705 gen_array_type_die (type, context_die);
24706 break;
24707
24708 case ENUMERAL_TYPE:
24709 case RECORD_TYPE:
24710 case UNION_TYPE:
24711 case QUAL_UNION_TYPE:
24712 gen_tagged_type_die (type, context_die, usage);
24713 return;
24714
24715 case VOID_TYPE:
24716 case INTEGER_TYPE:
24717 case REAL_TYPE:
24718 case FIXED_POINT_TYPE:
24719 case COMPLEX_TYPE:
24720 case BOOLEAN_TYPE:
24721 case POINTER_BOUNDS_TYPE:
24722 /* No DIEs needed for fundamental types. */
24723 break;
24724
24725 case NULLPTR_TYPE:
24726 case LANG_TYPE:
24727 /* Just use DW_TAG_unspecified_type. */
24728 {
24729 dw_die_ref type_die = lookup_type_die (type);
24730 if (type_die == NULL)
24731 {
24732 tree name = TYPE_IDENTIFIER (type);
24733 type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (),
24734 type);
24735 add_name_attribute (type_die, IDENTIFIER_POINTER (name));
24736 equate_type_number_to_die (type, type_die);
24737 }
24738 }
24739 break;
24740
24741 default:
24742 if (is_cxx_auto (type))
24743 {
24744 tree name = TYPE_IDENTIFIER (type);
24745 dw_die_ref *die = (name == get_identifier ("auto")
24746 ? &auto_die : &decltype_auto_die);
24747 if (!*die)
24748 {
24749 *die = new_die (DW_TAG_unspecified_type,
24750 comp_unit_die (), NULL_TREE);
24751 add_name_attribute (*die, IDENTIFIER_POINTER (name));
24752 }
24753 equate_type_number_to_die (type, *die);
24754 break;
24755 }
24756 gcc_unreachable ();
24757 }
24758
24759 TREE_ASM_WRITTEN (type) = 1;
24760}
24761
24762static void
24763gen_type_die (tree type, dw_die_ref context_die)
24764{
24765 if (type != error_mark_node)
24766 {
24767 gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
24768 if (flag_checking)
24769 {
24770 dw_die_ref die = lookup_type_die (type);
24771 if (die)
24772 check_die (die);
24773 }
24774 }
24775}
24776
24777/* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
24778 things which are local to the given block. */
24779
24780static void
24781gen_block_die (tree stmt, dw_die_ref context_die)
24782{
24783 int must_output_die = 0;
24784 bool inlined_func;
24785
24786 /* Ignore blocks that are NULL. */
24787 if (stmt == NULL_TREE)
24788 return;
24789
24790 inlined_func = inlined_function_outer_scope_p (stmt);
24791
24792 /* If the block is one fragment of a non-contiguous block, do not
24793 process the variables, since they will have been done by the
24794 origin block. Do process subblocks. */
24795 if (BLOCK_FRAGMENT_ORIGIN (stmt))
24796 {
24797 tree sub;
24798
24799 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
24800 gen_block_die (sub, context_die);
24801
24802 return;
24803 }
24804
24805 /* Determine if we need to output any Dwarf DIEs at all to represent this
24806 block. */
24807 if (inlined_func)
24808 /* The outer scopes for inlinings *must* always be represented. We
24809 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
24810 must_output_die = 1;
24811 else
24812 {
24813 /* Determine if this block directly contains any "significant"
24814 local declarations which we will need to output DIEs for. */
24815 if (debug_info_level > DINFO_LEVEL_TERSE)
24816 /* We are not in terse mode so *any* local declaration counts
24817 as being a "significant" one. */
24818 must_output_die = ((BLOCK_VARS (stmt) != NULL
24819 || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
24820 && (TREE_USED (stmt)
24821 || TREE_ASM_WRITTEN (stmt)
24822 || BLOCK_ABSTRACT (stmt)));
24823 else if ((TREE_USED (stmt)
24824 || TREE_ASM_WRITTEN (stmt)
24825 || BLOCK_ABSTRACT (stmt))
24826 && !dwarf2out_ignore_block (stmt))
24827 must_output_die = 1;
24828 }
24829
24830 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
24831 DIE for any block which contains no significant local declarations at
24832 all. Rather, in such cases we just call `decls_for_scope' so that any
24833 needed Dwarf info for any sub-blocks will get properly generated. Note
24834 that in terse mode, our definition of what constitutes a "significant"
24835 local declaration gets restricted to include only inlined function
24836 instances and local (nested) function definitions. */
24837 if (must_output_die)
24838 {
24839 if (inlined_func)
24840 {
24841 /* If STMT block is abstract, that means we have been called
24842 indirectly from dwarf2out_abstract_function.
24843 That function rightfully marks the descendent blocks (of
24844 the abstract function it is dealing with) as being abstract,
24845 precisely to prevent us from emitting any
24846 DW_TAG_inlined_subroutine DIE as a descendent
24847 of an abstract function instance. So in that case, we should
24848 not call gen_inlined_subroutine_die.
24849
24850 Later though, when cgraph asks dwarf2out to emit info
24851 for the concrete instance of the function decl into which
24852 the concrete instance of STMT got inlined, the later will lead
24853 to the generation of a DW_TAG_inlined_subroutine DIE. */
24854 if (! BLOCK_ABSTRACT (stmt))
24855 gen_inlined_subroutine_die (stmt, context_die);
24856 }
24857 else
24858 gen_lexical_block_die (stmt, context_die);
24859 }
24860 else
24861 decls_for_scope (stmt, context_die);
24862}
24863
24864/* Process variable DECL (or variable with origin ORIGIN) within
24865 block STMT and add it to CONTEXT_DIE. */
24866static void
24867process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
24868{
24869 dw_die_ref die;
24870 tree decl_or_origin = decl ? decl : origin;
24871
24872 if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
24873 die = lookup_decl_die (decl_or_origin);
24874 else if (TREE_CODE (decl_or_origin) == TYPE_DECL)
24875 {
24876 if (TYPE_DECL_IS_STUB (decl_or_origin))
24877 die = lookup_type_die (TREE_TYPE (decl_or_origin));
24878 else
24879 die = lookup_decl_die (decl_or_origin);
24880 /* Avoid re-creating the DIE late if it was optimized as unused early. */
24881 if (! die && ! early_dwarf)
24882 return;
24883 }
24884 else
24885 die = NULL;
24886
24887 /* Avoid creating DIEs for local typedefs and concrete static variables that
24888 will only be pruned later. */
24889 if ((origin || decl_ultimate_origin (decl))
24890 && (TREE_CODE (decl_or_origin) == TYPE_DECL
24891 || (VAR_P (decl_or_origin) && TREE_STATIC (decl_or_origin))))
24892 {
24893 origin = decl_ultimate_origin (decl_or_origin);
24894 if (decl && VAR_P (decl) && die != NULL)
24895 {
24896 die = lookup_decl_die (origin);
24897 if (die != NULL)
24898 equate_decl_number_to_die (decl, die);
24899 }
24900 return;
24901 }
24902
24903 if (die != NULL && die->die_parent == NULL)
24904 add_child_die (context_die, die);
24905 else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
24906 {
24907 if (early_dwarf)
24908 dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
24909 stmt, context_die);
24910 }
24911 else
24912 gen_decl_die (decl, origin, NULL, context_die);
24913}
24914
24915/* Generate all of the decls declared within a given scope and (recursively)
24916 all of its sub-blocks. */
24917
24918static void
24919decls_for_scope (tree stmt, dw_die_ref context_die)
24920{
24921 tree decl;
24922 unsigned int i;
24923 tree subblocks;
24924
24925 /* Ignore NULL blocks. */
24926 if (stmt == NULL_TREE)
24927 return;
24928
24929 /* Output the DIEs to represent all of the data objects and typedefs
24930 declared directly within this block but not within any nested
24931 sub-blocks. Also, nested function and tag DIEs have been
24932 generated with a parent of NULL; fix that up now. We don't
24933 have to do this if we're at -g1. */
24934 if (debug_info_level > DINFO_LEVEL_TERSE)
24935 {
24936 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
24937 process_scope_var (stmt, decl, NULL_TREE, context_die);
24938 /* BLOCK_NONLOCALIZED_VARs simply generate DIE stubs with abstract
24939 origin - avoid doing this twice as we have no good way to see
24940 if we've done it once already. */
24941 if (! early_dwarf)
24942 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
24943 {
24944 decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
24945 if (decl == current_function_decl)
24946 /* Ignore declarations of the current function, while they
24947 are declarations, gen_subprogram_die would treat them
24948 as definitions again, because they are equal to
24949 current_function_decl and endlessly recurse. */;
24950 else if (TREE_CODE (decl) == FUNCTION_DECL)
24951 process_scope_var (stmt, decl, NULL_TREE, context_die);
24952 else
24953 process_scope_var (stmt, NULL_TREE, decl, context_die);
24954 }
24955 }
24956
24957 /* Even if we're at -g1, we need to process the subblocks in order to get
24958 inlined call information. */
24959
24960 /* Output the DIEs to represent all sub-blocks (and the items declared
24961 therein) of this block. */
24962 for (subblocks = BLOCK_SUBBLOCKS (stmt);
24963 subblocks != NULL;
24964 subblocks = BLOCK_CHAIN (subblocks))
24965 gen_block_die (subblocks, context_die);
24966}
24967
24968/* Is this a typedef we can avoid emitting? */
24969
24970bool
24971is_redundant_typedef (const_tree decl)
24972{
24973 if (TYPE_DECL_IS_STUB (decl))
24974 return true;
24975
24976 if (DECL_ARTIFICIAL (decl)
24977 && DECL_CONTEXT (decl)
24978 && is_tagged_type (DECL_CONTEXT (decl))
24979 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
24980 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
24981 /* Also ignore the artificial member typedef for the class name. */
24982 return true;
24983
24984 return false;
24985}
24986
24987/* Return TRUE if TYPE is a typedef that names a type for linkage
24988 purposes. This kind of typedefs is produced by the C++ FE for
24989 constructs like:
24990
24991 typedef struct {...} foo;
24992
24993 In that case, there is no typedef variant type produced for foo.
24994 Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
24995 struct type. */
24996
24997static bool
24998is_naming_typedef_decl (const_tree decl)
24999{
25000 if (decl == NULL_TREE
25001 || TREE_CODE (decl) != TYPE_DECL
25002 || DECL_NAMELESS (decl)
25003 || !is_tagged_type (TREE_TYPE (decl))
25004 || DECL_IS_BUILTIN (decl)
25005 || is_redundant_typedef (decl)
25006 /* It looks like Ada produces TYPE_DECLs that are very similar
25007 to C++ naming typedefs but that have different
25008 semantics. Let's be specific to c++ for now. */
25009 || !is_cxx (decl))
25010 return FALSE;
25011
25012 return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
25013 && TYPE_NAME (TREE_TYPE (decl)) == decl
25014 && (TYPE_STUB_DECL (TREE_TYPE (decl))
25015 != TYPE_NAME (TREE_TYPE (decl))));
25016}
25017
25018/* Looks up the DIE for a context. */
25019
25020static inline dw_die_ref
25021lookup_context_die (tree context)
25022{
25023 if (context)
25024 {
25025 /* Find die that represents this context. */
25026 if (TYPE_P (context))
25027 {
25028 context = TYPE_MAIN_VARIANT (context);
25029 dw_die_ref ctx = lookup_type_die (context);
25030 if (!ctx)
25031 return NULL;
25032 return strip_naming_typedef (context, ctx);
25033 }
25034 else
25035 return lookup_decl_die (context);
25036 }
25037 return comp_unit_die ();
25038}
25039
25040/* Returns the DIE for a context. */
25041
25042static inline dw_die_ref
25043get_context_die (tree context)
25044{
25045 if (context)
25046 {
25047 /* Find die that represents this context. */
25048 if (TYPE_P (context))
25049 {
25050 context = TYPE_MAIN_VARIANT (context);
25051 return strip_naming_typedef (context, force_type_die (context));
25052 }
25053 else
25054 return force_decl_die (context);
25055 }
25056 return comp_unit_die ();
25057}
25058
25059/* Returns the DIE for decl. A DIE will always be returned. */
25060
25061static dw_die_ref
25062force_decl_die (tree decl)
25063{
25064 dw_die_ref decl_die;
25065 unsigned saved_external_flag;
25066 tree save_fn = NULL_TREE;
25067 decl_die = lookup_decl_die (decl);
25068 if (!decl_die)
25069 {
25070 dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
25071
25072 decl_die = lookup_decl_die (decl);
25073 if (decl_die)
25074 return decl_die;
25075
25076 switch (TREE_CODE (decl))
25077 {
25078 case FUNCTION_DECL:
25079 /* Clear current_function_decl, so that gen_subprogram_die thinks
25080 that this is a declaration. At this point, we just want to force
25081 declaration die. */
25082 save_fn = current_function_decl;
25083 current_function_decl = NULL_TREE;
25084 gen_subprogram_die (decl, context_die);
25085 current_function_decl = save_fn;
25086 break;
25087
25088 case VAR_DECL:
25089 /* Set external flag to force declaration die. Restore it after
25090 gen_decl_die() call. */
25091 saved_external_flag = DECL_EXTERNAL (decl);
25092 DECL_EXTERNAL (decl) = 1;
25093 gen_decl_die (decl, NULL, NULL, context_die);
25094 DECL_EXTERNAL (decl) = saved_external_flag;
25095 break;
25096
25097 case NAMESPACE_DECL:
25098 if (dwarf_version >= 3 || !dwarf_strict)
25099 dwarf2out_decl (decl);
25100 else
25101 /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace. */
25102 decl_die = comp_unit_die ();
25103 break;
25104
25105 case TRANSLATION_UNIT_DECL:
25106 decl_die = comp_unit_die ();
25107 break;
25108
25109 default:
25110 gcc_unreachable ();
25111 }
25112
25113 /* We should be able to find the DIE now. */
25114 if (!decl_die)
25115 decl_die = lookup_decl_die (decl);
25116 gcc_assert (decl_die);
25117 }
25118
25119 return decl_die;
25120}
25121
25122/* Returns the DIE for TYPE, that must not be a base type. A DIE is
25123 always returned. */
25124
25125static dw_die_ref
25126force_type_die (tree type)
25127{
25128 dw_die_ref type_die;
25129
25130 type_die = lookup_type_die (type);
25131 if (!type_die)
25132 {
25133 dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
25134
25135 type_die = modified_type_die (type, TYPE_QUALS_NO_ADDR_SPACE (type),
25136 false, context_die);
25137 gcc_assert (type_die);
25138 }
25139 return type_die;
25140}
25141
25142/* Force out any required namespaces to be able to output DECL,
25143 and return the new context_die for it, if it's changed. */
25144
25145static dw_die_ref
25146setup_namespace_context (tree thing, dw_die_ref context_die)
25147{
25148 tree context = (DECL_P (thing)
25149 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
25150 if (context && TREE_CODE (context) == NAMESPACE_DECL)
25151 /* Force out the namespace. */
25152 context_die = force_decl_die (context);
25153
25154 return context_die;
25155}
25156
25157/* Emit a declaration DIE for THING (which is either a DECL or a tagged
25158 type) within its namespace, if appropriate.
25159
25160 For compatibility with older debuggers, namespace DIEs only contain
25161 declarations; all definitions are emitted at CU scope, with
25162 DW_AT_specification pointing to the declaration (like with class
25163 members). */
25164
25165static dw_die_ref
25166declare_in_namespace (tree thing, dw_die_ref context_die)
25167{
25168 dw_die_ref ns_context;
25169
25170 if (debug_info_level <= DINFO_LEVEL_TERSE)
25171 return context_die;
25172
25173 /* External declarations in the local scope only need to be emitted
25174 once, not once in the namespace and once in the scope.
25175
25176 This avoids declaring the `extern' below in the
25177 namespace DIE as well as in the innermost scope:
25178
25179 namespace S
25180 {
25181 int i=5;
25182 int foo()
25183 {
25184 int i=8;
25185 extern int i;
25186 return i;
25187 }
25188 }
25189 */
25190 if (DECL_P (thing) && DECL_EXTERNAL (thing) && local_scope_p (context_die))
25191 return context_die;
25192
25193 /* If this decl is from an inlined function, then don't try to emit it in its
25194 namespace, as we will get confused. It would have already been emitted
25195 when the abstract instance of the inline function was emitted anyways. */
25196 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
25197 return context_die;
25198
25199 ns_context = setup_namespace_context (thing, context_die);
25200
25201 if (ns_context != context_die)
25202 {
25203 if (is_fortran ())
25204 return ns_context;
25205 if (DECL_P (thing))
25206 gen_decl_die (thing, NULL, NULL, ns_context);
25207 else
25208 gen_type_die (thing, ns_context);
25209 }
25210 return context_die;
25211}
25212
25213/* Generate a DIE for a namespace or namespace alias. */
25214
25215static void
25216gen_namespace_die (tree decl, dw_die_ref context_die)
25217{
25218 dw_die_ref namespace_die;
25219
25220 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
25221 they are an alias of. */
25222 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
25223 {
25224 /* Output a real namespace or module. */
25225 context_die = setup_namespace_context (decl, comp_unit_die ());
25226 namespace_die = new_die (is_fortran ()
25227 ? DW_TAG_module : DW_TAG_namespace,
25228 context_die, decl);
25229 /* For Fortran modules defined in different CU don't add src coords. */
25230 if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
25231 {
25232 const char *name = dwarf2_name (decl, 0);
25233 if (name)
25234 add_name_attribute (namespace_die, name);
25235 }
25236 else
25237 add_name_and_src_coords_attributes (namespace_die, decl);
25238 if (DECL_EXTERNAL (decl))
25239 add_AT_flag (namespace_die, DW_AT_declaration, 1);
25240 equate_decl_number_to_die (decl, namespace_die);
25241 }
25242 else
25243 {
25244 /* Output a namespace alias. */
25245
25246 /* Force out the namespace we are an alias of, if necessary. */
25247 dw_die_ref origin_die
25248 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
25249
25250 if (DECL_FILE_SCOPE_P (decl)
25251 || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
25252 context_die = setup_namespace_context (decl, comp_unit_die ());
25253 /* Now create the namespace alias DIE. */
25254 namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
25255 add_name_and_src_coords_attributes (namespace_die, decl);
25256 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
25257 equate_decl_number_to_die (decl, namespace_die);
25258 }
25259 /* Bypass dwarf2_name's check for DECL_NAMELESS. */
25260 if (want_pubnames ())
25261 add_pubname_string (lang_hooks.dwarf_name (decl, 1), namespace_die);
25262}
25263
25264/* Generate Dwarf debug information for a decl described by DECL.
25265 The return value is currently only meaningful for PARM_DECLs,
25266 for all other decls it returns NULL.
25267
25268 If DECL is a FIELD_DECL, CTX is required: see the comment for VLR_CONTEXT.
25269 It can be NULL otherwise. */
25270
25271static dw_die_ref
25272gen_decl_die (tree decl, tree origin, struct vlr_context *ctx,
25273 dw_die_ref context_die)
25274{
25275 tree decl_or_origin = decl ? decl : origin;
25276 tree class_origin = NULL, ultimate_origin;
25277
25278 if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
25279 return NULL;
25280
25281 /* Ignore pointer bounds decls. */
25282 if (DECL_P (decl_or_origin)
25283 && TREE_TYPE (decl_or_origin)
25284 && POINTER_BOUNDS_P (decl_or_origin))
25285 return NULL;
25286
25287 switch (TREE_CODE (decl_or_origin))
25288 {
25289 case ERROR_MARK:
25290 break;
25291
25292 case CONST_DECL:
25293 if (!is_fortran () && !is_ada ())
25294 {
25295 /* The individual enumerators of an enum type get output when we output
25296 the Dwarf representation of the relevant enum type itself. */
25297 break;
25298 }
25299
25300 /* Emit its type. */
25301 gen_type_die (TREE_TYPE (decl), context_die);
25302
25303 /* And its containing namespace. */
25304 context_die = declare_in_namespace (decl, context_die);
25305
25306 gen_const_die (decl, context_die);
25307 break;
25308
25309 case FUNCTION_DECL:
25310 /* Don't output any DIEs to represent mere function declarations,
25311 unless they are class members or explicit block externs. */
25312 if (DECL_INITIAL (decl_or_origin) == NULL_TREE
25313 && DECL_FILE_SCOPE_P (decl_or_origin)
25314 && (current_function_decl == NULL_TREE
25315 || DECL_ARTIFICIAL (decl_or_origin)))
25316 break;
25317
25318#if 0
25319 /* FIXME */
25320 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
25321 on local redeclarations of global functions. That seems broken. */
25322 if (current_function_decl != decl)
25323 /* This is only a declaration. */;
25324#endif
25325
25326 /* If we're emitting a clone, emit info for the abstract instance. */
25327 if (origin || DECL_ORIGIN (decl) != decl)
25328 dwarf2out_abstract_function (origin
25329 ? DECL_ORIGIN (origin)
25330 : DECL_ABSTRACT_ORIGIN (decl));
25331
25332 /* If we're emitting an out-of-line copy of an inline function,
25333 emit info for the abstract instance and set up to refer to it. */
25334 else if (cgraph_function_possibly_inlined_p (decl)
25335 && ! DECL_ABSTRACT_P (decl)
25336 && ! class_or_namespace_scope_p (context_die)
25337 /* dwarf2out_abstract_function won't emit a die if this is just
25338 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
25339 that case, because that works only if we have a die. */
25340 && DECL_INITIAL (decl) != NULL_TREE)
25341 {
25342 dwarf2out_abstract_function (decl);
25343 set_decl_origin_self (decl);
25344 }
25345
25346 /* Otherwise we're emitting the primary DIE for this decl. */
25347 else if (debug_info_level > DINFO_LEVEL_TERSE)
25348 {
25349 /* Before we describe the FUNCTION_DECL itself, make sure that we
25350 have its containing type. */
25351 if (!origin)
25352 origin = decl_class_context (decl);
25353 if (origin != NULL_TREE)
25354 gen_type_die (origin, context_die);
25355
25356 /* And its return type. */
25357 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
25358
25359 /* And its virtual context. */
25360 if (DECL_VINDEX (decl) != NULL_TREE)
25361 gen_type_die (DECL_CONTEXT (decl), context_die);
25362
25363 /* Make sure we have a member DIE for decl. */
25364 if (origin != NULL_TREE)
25365 gen_type_die_for_member (origin, decl, context_die);
25366
25367 /* And its containing namespace. */
25368 context_die = declare_in_namespace (decl, context_die);
25369 }
25370
25371 /* Now output a DIE to represent the function itself. */
25372 if (decl)
25373 gen_subprogram_die (decl, context_die);
25374 break;
25375
25376 case TYPE_DECL:
25377 /* If we are in terse mode, don't generate any DIEs to represent any
25378 actual typedefs. */
25379 if (debug_info_level <= DINFO_LEVEL_TERSE)
25380 break;
25381
25382 /* In the special case of a TYPE_DECL node representing the declaration
25383 of some type tag, if the given TYPE_DECL is marked as having been
25384 instantiated from some other (original) TYPE_DECL node (e.g. one which
25385 was generated within the original definition of an inline function) we
25386 used to generate a special (abbreviated) DW_TAG_structure_type,
25387 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. But nothing
25388 should be actually referencing those DIEs, as variable DIEs with that
25389 type would be emitted already in the abstract origin, so it was always
25390 removed during unused type prunning. Don't add anything in this
25391 case. */
25392 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
25393 break;
25394
25395 if (is_redundant_typedef (decl))
25396 gen_type_die (TREE_TYPE (decl), context_die);
25397 else
25398 /* Output a DIE to represent the typedef itself. */
25399 gen_typedef_die (decl, context_die);
25400 break;
25401
25402 case LABEL_DECL:
25403 if (debug_info_level >= DINFO_LEVEL_NORMAL)
25404 gen_label_die (decl, context_die);
25405 break;
25406
25407 case VAR_DECL:
25408 case RESULT_DECL:
25409 /* If we are in terse mode, don't generate any DIEs to represent any
25410 variable declarations or definitions. */
25411 if (debug_info_level <= DINFO_LEVEL_TERSE)
25412 break;
25413
25414 /* Output any DIEs that are needed to specify the type of this data
25415 object. */
25416 if (decl_by_reference_p (decl_or_origin))
25417 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
25418 else
25419 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
25420
25421 /* And its containing type. */
25422 class_origin = decl_class_context (decl_or_origin);
25423 if (class_origin != NULL_TREE)
25424 gen_type_die_for_member (class_origin, decl_or_origin, context_die);
25425
25426 /* And its containing namespace. */
25427 context_die = declare_in_namespace (decl_or_origin, context_die);
25428
25429 /* Now output the DIE to represent the data object itself. This gets
25430 complicated because of the possibility that the VAR_DECL really
25431 represents an inlined instance of a formal parameter for an inline
25432 function. */
25433 ultimate_origin = decl_ultimate_origin (decl_or_origin);
25434 if (ultimate_origin != NULL_TREE
25435 && TREE_CODE (ultimate_origin) == PARM_DECL)
25436 gen_formal_parameter_die (decl, origin,
25437 true /* Emit name attribute. */,
25438 context_die);
25439 else
25440 gen_variable_die (decl, origin, context_die);
25441 break;
25442
25443 case FIELD_DECL:
25444 gcc_assert (ctx != NULL && ctx->struct_type != NULL);
25445 /* Ignore the nameless fields that are used to skip bits but handle C++
25446 anonymous unions and structs. */
25447 if (DECL_NAME (decl) != NULL_TREE
25448 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
25449 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
25450 {
25451 gen_type_die (member_declared_type (decl), context_die);
25452 gen_field_die (decl, ctx, context_die);
25453 }
25454 break;
25455
25456 case PARM_DECL:
25457 if (DECL_BY_REFERENCE (decl_or_origin))
25458 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
25459 else
25460 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
25461 return gen_formal_parameter_die (decl, origin,
25462 true /* Emit name attribute. */,
25463 context_die);
25464
25465 case NAMESPACE_DECL:
25466 if (dwarf_version >= 3 || !dwarf_strict)
25467 gen_namespace_die (decl, context_die);
25468 break;
25469
25470 case IMPORTED_DECL:
25471 dwarf2out_imported_module_or_decl_1 (decl, DECL_NAME (decl),
25472 DECL_CONTEXT (decl), context_die);
25473 break;
25474
25475 case NAMELIST_DECL:
25476 gen_namelist_decl (DECL_NAME (decl), context_die,
25477 NAMELIST_DECL_ASSOCIATED_DECL (decl));
25478 break;
25479
25480 default:
25481 /* Probably some frontend-internal decl. Assume we don't care. */
25482 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
25483 break;
25484 }
25485
25486 return NULL;
25487}
25488
25489/* Output initial debug information for global DECL. Called at the
25490 end of the parsing process.
25491
25492 This is the initial debug generation process. As such, the DIEs
25493 generated may be incomplete. A later debug generation pass
25494 (dwarf2out_late_global_decl) will augment the information generated
25495 in this pass (e.g., with complete location info). */
25496
25497static void
25498dwarf2out_early_global_decl (tree decl)
25499{
25500 set_early_dwarf s;
25501
25502 /* gen_decl_die() will set DECL_ABSTRACT because
25503 cgraph_function_possibly_inlined_p() returns true. This is in
25504 turn will cause DW_AT_inline attributes to be set.
25505
25506 This happens because at early dwarf generation, there is no
25507 cgraph information, causing cgraph_function_possibly_inlined_p()
25508 to return true. Trick cgraph_function_possibly_inlined_p()
25509 while we generate dwarf early. */
25510 bool save = symtab->global_info_ready;
25511 symtab->global_info_ready = true;
25512
25513 /* We don't handle TYPE_DECLs. If required, they'll be reached via
25514 other DECLs and they can point to template types or other things
25515 that dwarf2out can't handle when done via dwarf2out_decl. */
25516 if (TREE_CODE (decl) != TYPE_DECL
25517 && TREE_CODE (decl) != PARM_DECL)
25518 {
25519 tree save_fndecl = current_function_decl;
25520 if (TREE_CODE (decl) == FUNCTION_DECL)
25521 {
25522 /* No cfun means the symbol has no body, so there's nothing
25523 to emit. */
25524 if (!DECL_STRUCT_FUNCTION (decl))
25525 goto early_decl_exit;
25526
25527 /* For nested functions, make sure we have DIEs for the parents first
25528 so that all nested DIEs are generated at the proper scope in the
25529 first shot. */
25530 tree context = decl_function_context (decl);
25531 if (context != NULL)
25532 {
25533 dw_die_ref context_die = lookup_decl_die (context);
25534 current_function_decl = context;
25535
25536 /* Avoid emitting DIEs multiple times, but still process CONTEXT
25537 enough so that it lands in its own context. This avoids type
25538 pruning issues later on. */
25539 if (context_die == NULL || is_declaration_die (context_die))
25540 dwarf2out_decl (context);
25541 }
25542
25543 current_function_decl = decl;
25544 }
25545 dwarf2out_decl (decl);
25546 if (TREE_CODE (decl) == FUNCTION_DECL)
25547 current_function_decl = save_fndecl;
25548 }
25549 early_decl_exit:
25550 symtab->global_info_ready = save;
25551}
25552
25553/* Output debug information for global decl DECL. Called from
25554 toplev.c after compilation proper has finished. */
25555
25556static void
25557dwarf2out_late_global_decl (tree decl)
25558{
25559 /* Fill-in any location information we were unable to determine
25560 on the first pass. */
25561 if (VAR_P (decl) && !POINTER_BOUNDS_P (decl))
25562 {
25563 dw_die_ref die = lookup_decl_die (decl);
25564
25565 /* We have to generate early debug late for LTO. */
25566 if (! die && in_lto_p)
25567 {
25568 dwarf2out_decl (decl);
25569 die = lookup_decl_die (decl);
25570 }
25571
25572 if (die)
25573 {
25574 /* We get called via the symtab code invoking late_global_decl
25575 for symbols that are optimized out. Do not add locations
25576 for those. */
25577 varpool_node *node = varpool_node::get (decl);
25578 if (! node || ! node->definition)
25579 tree_add_const_value_attribute_for_decl (die, decl);
25580 else
25581 add_location_or_const_value_attribute (die, decl, false);
25582 }
25583 }
25584}
25585
25586/* Output debug information for type decl DECL. Called from toplev.c
25587 and from language front ends (to record built-in types). */
25588static void
25589dwarf2out_type_decl (tree decl, int local)
25590{
25591 if (!local)
25592 {
25593 set_early_dwarf s;
25594 dwarf2out_decl (decl);
25595 }
25596}
25597
25598/* Output debug information for imported module or decl DECL.
25599 NAME is non-NULL name in the lexical block if the decl has been renamed.
25600 LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
25601 that DECL belongs to.
25602 LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK. */
25603static void
25604dwarf2out_imported_module_or_decl_1 (tree decl,
25605 tree name,
25606 tree lexical_block,
25607 dw_die_ref lexical_block_die)
25608{
25609 expanded_location xloc;
25610 dw_die_ref imported_die = NULL;
25611 dw_die_ref at_import_die;
25612
25613 if (TREE_CODE (decl) == IMPORTED_DECL)
25614 {
25615 xloc = expand_location (DECL_SOURCE_LOCATION (decl));
25616 decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
25617 gcc_assert (decl);
25618 }
25619 else
25620 xloc = expand_location (input_location);
25621
25622 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
25623 {
25624 at_import_die = force_type_die (TREE_TYPE (decl));
25625 /* For namespace N { typedef void T; } using N::T; base_type_die
25626 returns NULL, but DW_TAG_imported_declaration requires
25627 the DW_AT_import tag. Force creation of DW_TAG_typedef. */
25628 if (!at_import_die)
25629 {
25630 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
25631 gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
25632 at_import_die = lookup_type_die (TREE_TYPE (decl));
25633 gcc_assert (at_import_die);
25634 }
25635 }
25636 else
25637 {
25638 at_import_die = lookup_decl_die (decl);
25639 if (!at_import_die)
25640 {
25641 /* If we're trying to avoid duplicate debug info, we may not have
25642 emitted the member decl for this field. Emit it now. */
25643 if (TREE_CODE (decl) == FIELD_DECL)
25644 {
25645 tree type = DECL_CONTEXT (decl);
25646
25647 if (TYPE_CONTEXT (type)
25648 && TYPE_P (TYPE_CONTEXT (type))
25649 && !should_emit_struct_debug (TYPE_CONTEXT (type),
25650 DINFO_USAGE_DIR_USE))
25651 return;
25652 gen_type_die_for_member (type, decl,
25653 get_context_die (TYPE_CONTEXT (type)));
25654 }
25655 if (TREE_CODE (decl) == NAMELIST_DECL)
25656 at_import_die = gen_namelist_decl (DECL_NAME (decl),
25657 get_context_die (DECL_CONTEXT (decl)),
25658 NULL_TREE);
25659 else
25660 at_import_die = force_decl_die (decl);
25661 }
25662 }
25663
25664 if (TREE_CODE (decl) == NAMESPACE_DECL)
25665 {
25666 if (dwarf_version >= 3 || !dwarf_strict)
25667 imported_die = new_die (DW_TAG_imported_module,
25668 lexical_block_die,
25669 lexical_block);
25670 else
25671 return;
25672 }
25673 else
25674 imported_die = new_die (DW_TAG_imported_declaration,
25675 lexical_block_die,
25676 lexical_block);
25677
25678 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
25679 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
25680 if (debug_column_info && xloc.column)
25681 add_AT_unsigned (imported_die, DW_AT_decl_column, xloc.column);
25682 if (name)
25683 add_AT_string (imported_die, DW_AT_name,
25684 IDENTIFIER_POINTER (name));
25685 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
25686}
25687
25688/* Output debug information for imported module or decl DECL.
25689 NAME is non-NULL name in context if the decl has been renamed.
25690 CHILD is true if decl is one of the renamed decls as part of
25691 importing whole module. */
25692
25693static void
25694dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
25695 bool child)
25696{
25697 /* dw_die_ref at_import_die; */
25698 dw_die_ref scope_die;
25699
25700 if (debug_info_level <= DINFO_LEVEL_TERSE)
25701 return;
25702
25703 gcc_assert (decl);
25704
25705 set_early_dwarf s;
25706
25707 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
25708 We need decl DIE for reference and scope die. First, get DIE for the decl
25709 itself. */
25710
25711 /* Get the scope die for decl context. Use comp_unit_die for global module
25712 or decl. If die is not found for non globals, force new die. */
25713 if (context
25714 && TYPE_P (context)
25715 && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
25716 return;
25717
25718 scope_die = get_context_die (context);
25719
25720 if (child)
25721 {
25722 /* DW_TAG_imported_module was introduced in the DWARFv3 specification, so
25723 there is nothing we can do, here. */
25724 if (dwarf_version < 3 && dwarf_strict)
25725 return;
25726
25727 gcc_assert (scope_die->die_child);
25728 gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
25729 gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
25730 scope_die = scope_die->die_child;
25731 }
25732
25733 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
25734 dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
25735}
25736
25737/* Output debug information for namelists. */
25738
25739static dw_die_ref
25740gen_namelist_decl (tree name, dw_die_ref scope_die, tree item_decls)
25741{
25742 dw_die_ref nml_die, nml_item_die, nml_item_ref_die;
25743 tree value;
25744 unsigned i;
25745
25746 if (debug_info_level <= DINFO_LEVEL_TERSE)
25747 return NULL;
25748
25749 gcc_assert (scope_die != NULL);
25750 nml_die = new_die (DW_TAG_namelist, scope_die, NULL);
25751 add_AT_string (nml_die, DW_AT_name, IDENTIFIER_POINTER (name));
25752
25753 /* If there are no item_decls, we have a nondefining namelist, e.g.
25754 with USE association; hence, set DW_AT_declaration. */
25755 if (item_decls == NULL_TREE)
25756 {
25757 add_AT_flag (nml_die, DW_AT_declaration, 1);
25758 return nml_die;
25759 }
25760
25761 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (item_decls), i, value)
25762 {
25763 nml_item_ref_die = lookup_decl_die (value);
25764 if (!nml_item_ref_die)
25765 nml_item_ref_die = force_decl_die (value);
25766
25767 nml_item_die = new_die (DW_TAG_namelist_item, nml_die, NULL);
25768 add_AT_die_ref (nml_item_die, DW_AT_namelist_items, nml_item_ref_die);
25769 }
25770 return nml_die;
25771}
25772
25773
25774/* Write the debugging output for DECL and return the DIE. */
25775
25776static void
25777dwarf2out_decl (tree decl)
25778{
25779 dw_die_ref context_die = comp_unit_die ();
25780
25781 switch (TREE_CODE (decl))
25782 {
25783 case ERROR_MARK:
25784 return;
25785
25786 case FUNCTION_DECL:
25787 /* What we would really like to do here is to filter out all mere
25788 file-scope declarations of file-scope functions which are never
25789 referenced later within this translation unit (and keep all of ones
25790 that *are* referenced later on) but we aren't clairvoyant, so we have
25791 no idea which functions will be referenced in the future (i.e. later
25792 on within the current translation unit). So here we just ignore all
25793 file-scope function declarations which are not also definitions. If
25794 and when the debugger needs to know something about these functions,
25795 it will have to hunt around and find the DWARF information associated
25796 with the definition of the function.
25797
25798 We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
25799 nodes represent definitions and which ones represent mere
25800 declarations. We have to check DECL_INITIAL instead. That's because
25801 the C front-end supports some weird semantics for "extern inline"
25802 function definitions. These can get inlined within the current
25803 translation unit (and thus, we need to generate Dwarf info for their
25804 abstract instances so that the Dwarf info for the concrete inlined
25805 instances can have something to refer to) but the compiler never
25806 generates any out-of-lines instances of such things (despite the fact
25807 that they *are* definitions).
25808
25809 The important point is that the C front-end marks these "extern
25810 inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
25811 them anyway. Note that the C++ front-end also plays some similar games
25812 for inline function definitions appearing within include files which
25813 also contain `#pragma interface' pragmas.
25814
25815 If we are called from dwarf2out_abstract_function output a DIE
25816 anyway. We can end up here this way with early inlining and LTO
25817 where the inlined function is output in a different LTRANS unit
25818 or not at all. */
25819 if (DECL_INITIAL (decl) == NULL_TREE
25820 && ! DECL_ABSTRACT_P (decl))
25821 return;
25822
25823 /* If we're a nested function, initially use a parent of NULL; if we're
25824 a plain function, this will be fixed up in decls_for_scope. If
25825 we're a method, it will be ignored, since we already have a DIE. */
25826 if (decl_function_context (decl)
25827 /* But if we're in terse mode, we don't care about scope. */
25828 && debug_info_level > DINFO_LEVEL_TERSE)
25829 context_die = NULL;
25830 break;
25831
25832 case VAR_DECL:
25833 /* For local statics lookup proper context die. */
25834 if (local_function_static (decl))
25835 context_die = lookup_decl_die (DECL_CONTEXT (decl));
25836
25837 /* If we are in terse mode, don't generate any DIEs to represent any
25838 variable declarations or definitions. */
25839 if (debug_info_level <= DINFO_LEVEL_TERSE)
25840 return;
25841 break;
25842
25843 case CONST_DECL:
25844 if (debug_info_level <= DINFO_LEVEL_TERSE)
25845 return;
25846 if (!is_fortran () && !is_ada ())
25847 return;
25848 if (TREE_STATIC (decl) && decl_function_context (decl))
25849 context_die = lookup_decl_die (DECL_CONTEXT (decl));
25850 break;
25851
25852 case NAMESPACE_DECL:
25853 case IMPORTED_DECL:
25854 if (debug_info_level <= DINFO_LEVEL_TERSE)
25855 return;
25856 if (lookup_decl_die (decl) != NULL)
25857 return;
25858 break;
25859
25860 case TYPE_DECL:
25861 /* Don't emit stubs for types unless they are needed by other DIEs. */
25862 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
25863 return;
25864
25865 /* Don't bother trying to generate any DIEs to represent any of the
25866 normal built-in types for the language we are compiling. */
25867 if (DECL_IS_BUILTIN (decl))
25868 return;
25869
25870 /* If we are in terse mode, don't generate any DIEs for types. */
25871 if (debug_info_level <= DINFO_LEVEL_TERSE)
25872 return;
25873
25874 /* If we're a function-scope tag, initially use a parent of NULL;
25875 this will be fixed up in decls_for_scope. */
25876 if (decl_function_context (decl))
25877 context_die = NULL;
25878
25879 break;
25880
25881 case NAMELIST_DECL:
25882 break;
25883
25884 default:
25885 return;
25886 }
25887
25888 gen_decl_die (decl, NULL, NULL, context_die);
25889
25890 if (flag_checking)
25891 {
25892 dw_die_ref die = lookup_decl_die (decl);
25893 if (die)
25894 check_die (die);
25895 }
25896}
25897
25898/* Write the debugging output for DECL. */
25899
25900static void
25901dwarf2out_function_decl (tree decl)
25902{
25903 dwarf2out_decl (decl);
25904 call_arg_locations = NULL;
25905 call_arg_loc_last = NULL;
25906 call_site_count = -1;
25907 tail_call_site_count = -1;
25908 decl_loc_table->empty ();
25909 cached_dw_loc_list_table->empty ();
25910}
25911
25912/* Output a marker (i.e. a label) for the beginning of the generated code for
25913 a lexical block. */
25914
25915static void
25916dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
25917 unsigned int blocknum)
25918{
25919 switch_to_section (current_function_section ());
25920 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
25921}
25922
25923/* Output a marker (i.e. a label) for the end of the generated code for a
25924 lexical block. */
25925
25926static void
25927dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
25928{
25929 switch_to_section (current_function_section ());
25930 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
25931}
25932
25933/* Returns nonzero if it is appropriate not to emit any debugging
25934 information for BLOCK, because it doesn't contain any instructions.
25935
25936 Don't allow this for blocks with nested functions or local classes
25937 as we would end up with orphans, and in the presence of scheduling
25938 we may end up calling them anyway. */
25939
25940static bool
25941dwarf2out_ignore_block (const_tree block)
25942{
25943 tree decl;
25944 unsigned int i;
25945
25946 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
25947 if (TREE_CODE (decl) == FUNCTION_DECL
25948 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
25949 return 0;
25950 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
25951 {
25952 decl = BLOCK_NONLOCALIZED_VAR (block, i);
25953 if (TREE_CODE (decl) == FUNCTION_DECL
25954 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
25955 return 0;
25956 }
25957
25958 return 1;
25959}
25960
25961/* Hash table routines for file_hash. */
25962
25963bool
25964dwarf_file_hasher::equal (dwarf_file_data *p1, const char *p2)
25965{
25966 return filename_cmp (p1->filename, p2) == 0;
25967}
25968
25969hashval_t
25970dwarf_file_hasher::hash (dwarf_file_data *p)
25971{
25972 return htab_hash_string (p->filename);
25973}
25974
25975/* Lookup FILE_NAME (in the list of filenames that we know about here in
25976 dwarf2out.c) and return its "index". The index of each (known) filename is
25977 just a unique number which is associated with only that one filename. We
25978 need such numbers for the sake of generating labels (in the .debug_sfnames
25979 section) and references to those files numbers (in the .debug_srcinfo
25980 and .debug_macinfo sections). If the filename given as an argument is not
25981 found in our current list, add it to the list and assign it the next
25982 available unique index number. */
25983
25984static struct dwarf_file_data *
25985lookup_filename (const char *file_name)
25986{
25987 struct dwarf_file_data * created;
25988
25989 if (!file_name)
25990 return NULL;
25991
25992 dwarf_file_data **slot
25993 = file_table->find_slot_with_hash (file_name, htab_hash_string (file_name),
25994 INSERT);
25995 if (*slot)
25996 return *slot;
25997
25998 created = ggc_alloc<dwarf_file_data> ();
25999 created->filename = file_name;
26000 created->emitted_number = 0;
26001 *slot = created;
26002 return created;
26003}
26004
26005/* If the assembler will construct the file table, then translate the compiler
26006 internal file table number into the assembler file table number, and emit
26007 a .file directive if we haven't already emitted one yet. The file table
26008 numbers are different because we prune debug info for unused variables and
26009 types, which may include filenames. */
26010
26011static int
26012maybe_emit_file (struct dwarf_file_data * fd)
26013{
26014 if (! fd->emitted_number)
26015 {
26016 if (last_emitted_file)
26017 fd->emitted_number = last_emitted_file->emitted_number + 1;
26018 else
26019 fd->emitted_number = 1;
26020 last_emitted_file = fd;
26021
26022 if (DWARF2_ASM_LINE_DEBUG_INFO)
26023 {
26024 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
26025 output_quoted_string (asm_out_file,
26026 remap_debug_filename (fd->filename));
26027 fputc ('\n', asm_out_file);
26028 }
26029 }
26030
26031 return fd->emitted_number;
26032}
26033
26034/* Schedule generation of a DW_AT_const_value attribute to DIE.
26035 That generation should happen after function debug info has been
26036 generated. The value of the attribute is the constant value of ARG. */
26037
26038static void
26039append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
26040{
26041 die_arg_entry entry;
26042
26043 if (!die || !arg)
26044 return;
26045
26046 gcc_assert (early_dwarf);
26047
26048 if (!tmpl_value_parm_die_table)
26049 vec_alloc (tmpl_value_parm_die_table, 32);
26050
26051 entry.die = die;
26052 entry.arg = arg;
26053 vec_safe_push (tmpl_value_parm_die_table, entry);
26054}
26055
26056/* Return TRUE if T is an instance of generic type, FALSE
26057 otherwise. */
26058
26059static bool
26060generic_type_p (tree t)
26061{
26062 if (t == NULL_TREE || !TYPE_P (t))
26063 return false;
26064 return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
26065}
26066
26067/* Schedule the generation of the generic parameter dies for the
26068 instance of generic type T. The proper generation itself is later
26069 done by gen_scheduled_generic_parms_dies. */
26070
26071static void
26072schedule_generic_params_dies_gen (tree t)
26073{
26074 if (!generic_type_p (t))
26075 return;
26076
26077 gcc_assert (early_dwarf);
26078
26079 if (!generic_type_instances)
26080 vec_alloc (generic_type_instances, 256);
26081
26082 vec_safe_push (generic_type_instances, t);
26083}
26084
26085/* Add a DW_AT_const_value attribute to DIEs that were scheduled
26086 by append_entry_to_tmpl_value_parm_die_table. This function must
26087 be called after function DIEs have been generated. */
26088
26089static void
26090gen_remaining_tmpl_value_param_die_attribute (void)
26091{
26092 if (tmpl_value_parm_die_table)
26093 {
26094 unsigned i, j;
26095 die_arg_entry *e;
26096
26097 /* We do this in two phases - first get the cases we can
26098 handle during early-finish, preserving those we cannot
26099 (containing symbolic constants where we don't yet know
26100 whether we are going to output the referenced symbols).
26101 For those we try again at late-finish. */
26102 j = 0;
26103 FOR_EACH_VEC_ELT (*tmpl_value_parm_die_table, i, e)
26104 {
26105 if (!e->die->removed
26106 && !tree_add_const_value_attribute (e->die, e->arg))
26107 {
26108 dw_loc_descr_ref loc = NULL;
26109 if (! early_dwarf
26110 && (dwarf_version >= 5 || !dwarf_strict))
26111 loc = loc_descriptor_from_tree (e->arg, 2, NULL);
26112 if (loc)
26113 add_AT_loc (e->die, DW_AT_location, loc);
26114 else
26115 (*tmpl_value_parm_die_table)[j++] = *e;
26116 }
26117 }
26118 tmpl_value_parm_die_table->truncate (j);
26119 }
26120}
26121
26122/* Generate generic parameters DIEs for instances of generic types
26123 that have been previously scheduled by
26124 schedule_generic_params_dies_gen. This function must be called
26125 after all the types of the CU have been laid out. */
26126
26127static void
26128gen_scheduled_generic_parms_dies (void)
26129{
26130 unsigned i;
26131 tree t;
26132
26133 if (!generic_type_instances)
26134 return;
26135
26136 FOR_EACH_VEC_ELT (*generic_type_instances, i, t)
26137 if (COMPLETE_TYPE_P (t))
26138 gen_generic_params_dies (t);
26139
26140 generic_type_instances = NULL;
26141}
26142
26143
26144/* Replace DW_AT_name for the decl with name. */
26145
26146static void
26147dwarf2out_set_name (tree decl, tree name)
26148{
26149 dw_die_ref die;
26150 dw_attr_node *attr;
26151 const char *dname;
26152
26153 die = TYPE_SYMTAB_DIE (decl);
26154 if (!die)
26155 return;
26156
26157 dname = dwarf2_name (name, 0);
26158 if (!dname)
26159 return;
26160
26161 attr = get_AT (die, DW_AT_name);
26162 if (attr)
26163 {
26164 struct indirect_string_node *node;
26165
26166 node = find_AT_string (dname);
26167 /* replace the string. */
26168 attr->dw_attr_val.v.val_str = node;
26169 }
26170
26171 else
26172 add_name_attribute (die, dname);
26173}
26174
26175/* True if before or during processing of the first function being emitted. */
26176static bool in_first_function_p = true;
26177/* True if loc_note during dwarf2out_var_location call might still be
26178 before first real instruction at address equal to .Ltext0. */
26179static bool maybe_at_text_label_p = true;
26180/* One above highest N where .LVLN label might be equal to .Ltext0 label. */
26181static unsigned int first_loclabel_num_not_at_text_label;
26182
26183/* Called by the final INSN scan whenever we see a var location. We
26184 use it to drop labels in the right places, and throw the location in
26185 our lookup table. */
26186
26187static void
26188dwarf2out_var_location (rtx_insn *loc_note)
26189{
26190 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
26191 struct var_loc_node *newloc;
26192 rtx_insn *next_real, *next_note;
26193 rtx_insn *call_insn = NULL;
26194 static const char *last_label;
26195 static const char *last_postcall_label;
26196 static bool last_in_cold_section_p;
26197 static rtx_insn *expected_next_loc_note;
26198 tree decl;
26199 bool var_loc_p;
26200
26201 if (!NOTE_P (loc_note))
26202 {
26203 if (CALL_P (loc_note))
26204 {
26205 call_site_count++;
26206 if (SIBLING_CALL_P (loc_note))
26207 tail_call_site_count++;
26208 if (optimize == 0 && !flag_var_tracking)
26209 {
26210 /* When the var-tracking pass is not running, there is no note
26211 for indirect calls whose target is compile-time known. In this
26212 case, process such calls specifically so that we generate call
26213 sites for them anyway. */
26214 rtx x = PATTERN (loc_note);
26215 if (GET_CODE (x) == PARALLEL)
26216 x = XVECEXP (x, 0, 0);
26217 if (GET_CODE (x) == SET)
26218 x = SET_SRC (x);
26219 if (GET_CODE (x) == CALL)
26220 x = XEXP (x, 0);
26221 if (!MEM_P (x)
26222 || GET_CODE (XEXP (x, 0)) != SYMBOL_REF
26223 || !SYMBOL_REF_DECL (XEXP (x, 0))
26224 || (TREE_CODE (SYMBOL_REF_DECL (XEXP (x, 0)))
26225 != FUNCTION_DECL))
26226 {
26227 call_insn = loc_note;
26228 loc_note = NULL;
26229 var_loc_p = false;
26230
26231 next_real = next_real_insn (call_insn);
26232 next_note = NULL;
26233 cached_next_real_insn = NULL;
26234 goto create_label;
26235 }
26236 }
26237 }
26238 return;
26239 }
26240
26241 var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
26242 if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
26243 return;
26244
26245 /* Optimize processing a large consecutive sequence of location
26246 notes so we don't spend too much time in next_real_insn. If the
26247 next insn is another location note, remember the next_real_insn
26248 calculation for next time. */
26249 next_real = cached_next_real_insn;
26250 if (next_real)
26251 {
26252 if (expected_next_loc_note != loc_note)
26253 next_real = NULL;
26254 }
26255
26256 next_note = NEXT_INSN (loc_note);
26257 if (! next_note
26258 || next_note->deleted ()
26259 || ! NOTE_P (next_note)
26260 || (NOTE_KIND (next_note) != NOTE_INSN_VAR_LOCATION
26261 && NOTE_KIND (next_note) != NOTE_INSN_CALL_ARG_LOCATION))
26262 next_note = NULL;
26263
26264 if (! next_real)
26265 next_real = next_real_insn (loc_note);
26266
26267 if (next_note)
26268 {
26269 expected_next_loc_note = next_note;
26270 cached_next_real_insn = next_real;
26271 }
26272 else
26273 cached_next_real_insn = NULL;
26274
26275 /* If there are no instructions which would be affected by this note,
26276 don't do anything. */
26277 if (var_loc_p
26278 && next_real == NULL_RTX
26279 && !NOTE_DURING_CALL_P (loc_note))
26280 return;
26281
26282create_label:
26283
26284 if (next_real == NULL_RTX)
26285 next_real = get_last_insn ();
26286
26287 /* If there were any real insns between note we processed last time
26288 and this note (or if it is the first note), clear
26289 last_{,postcall_}label so that they are not reused this time. */
26290 if (last_var_location_insn == NULL_RTX
26291 || last_var_location_insn != next_real
26292 || last_in_cold_section_p != in_cold_section_p)
26293 {
26294 last_label = NULL;
26295 last_postcall_label = NULL;
26296 }
26297
26298 if (var_loc_p)
26299 {
26300 decl = NOTE_VAR_LOCATION_DECL (loc_note);
26301 newloc = add_var_loc_to_decl (decl, loc_note,
26302 NOTE_DURING_CALL_P (loc_note)
26303 ? last_postcall_label : last_label);
26304 if (newloc == NULL)
26305 return;
26306 }
26307 else
26308 {
26309 decl = NULL_TREE;
26310 newloc = NULL;
26311 }
26312
26313 /* If there were no real insns between note we processed last time
26314 and this note, use the label we emitted last time. Otherwise
26315 create a new label and emit it. */
26316 if (last_label == NULL)
26317 {
26318 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
26319 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
26320 loclabel_num++;
26321 last_label = ggc_strdup (loclabel);
26322 /* See if loclabel might be equal to .Ltext0. If yes,
26323 bump first_loclabel_num_not_at_text_label. */
26324 if (!have_multiple_function_sections
26325 && in_first_function_p
26326 && maybe_at_text_label_p)
26327 {
26328 static rtx_insn *last_start;
26329 rtx_insn *insn;
26330 for (insn = loc_note; insn; insn = previous_insn (insn))
26331 if (insn == last_start)
26332 break;
26333 else if (!NONDEBUG_INSN_P (insn))
26334 continue;
26335 else
26336 {
26337 rtx body = PATTERN (insn);
26338 if (GET_CODE (body) == USE || GET_CODE (body) == CLOBBER)
26339 continue;
26340 /* Inline asm could occupy zero bytes. */
26341 else if (GET_CODE (body) == ASM_INPUT
26342 || asm_noperands (body) >= 0)
26343 continue;
26344#ifdef HAVE_attr_length
26345 else if (get_attr_min_length (insn) == 0)
26346 continue;
26347#endif
26348 else
26349 {
26350 /* Assume insn has non-zero length. */
26351 maybe_at_text_label_p = false;
26352 break;
26353 }
26354 }
26355 if (maybe_at_text_label_p)
26356 {
26357 last_start = loc_note;
26358 first_loclabel_num_not_at_text_label = loclabel_num;
26359 }
26360 }
26361 }
26362
26363 gcc_assert ((loc_note == NULL_RTX && call_insn != NULL_RTX)
26364 || (loc_note != NULL_RTX && call_insn == NULL_RTX));
26365
26366 if (!var_loc_p)
26367 {
26368 struct call_arg_loc_node *ca_loc
26369 = ggc_cleared_alloc<call_arg_loc_node> ();
26370 rtx_insn *prev
26371 = loc_note != NULL_RTX ? prev_real_insn (loc_note) : call_insn;
26372
26373 ca_loc->call_arg_loc_note = loc_note;
26374 ca_loc->next = NULL;
26375 ca_loc->label = last_label;
26376 gcc_assert (prev
26377 && (CALL_P (prev)
26378 || (NONJUMP_INSN_P (prev)
26379 && GET_CODE (PATTERN (prev)) == SEQUENCE
26380 && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
26381 if (!CALL_P (prev))
26382 prev = as_a <rtx_sequence *> (PATTERN (prev))->insn (0);
26383 ca_loc->tail_call_p = SIBLING_CALL_P (prev);
26384
26385 /* Look for a SYMBOL_REF in the "prev" instruction. */
26386 rtx x = get_call_rtx_from (PATTERN (prev));
26387 if (x)
26388 {
26389 /* Try to get the call symbol, if any. */
26390 if (MEM_P (XEXP (x, 0)))
26391 x = XEXP (x, 0);
26392 /* First, look for a memory access to a symbol_ref. */
26393 if (GET_CODE (XEXP (x, 0)) == SYMBOL_REF
26394 && SYMBOL_REF_DECL (XEXP (x, 0))
26395 && TREE_CODE (SYMBOL_REF_DECL (XEXP (x, 0))) == FUNCTION_DECL)
26396 ca_loc->symbol_ref = XEXP (x, 0);
26397 /* Otherwise, look at a compile-time known user-level function
26398 declaration. */
26399 else if (MEM_P (x)
26400 && MEM_EXPR (x)
26401 && TREE_CODE (MEM_EXPR (x)) == FUNCTION_DECL)
26402 ca_loc->symbol_ref = XEXP (DECL_RTL (MEM_EXPR (x)), 0);
26403 }
26404
26405 ca_loc->block = insn_scope (prev);
26406 if (call_arg_locations)
26407 call_arg_loc_last->next = ca_loc;
26408 else
26409 call_arg_locations = ca_loc;
26410 call_arg_loc_last = ca_loc;
26411 }
26412 else if (loc_note != NULL_RTX && !NOTE_DURING_CALL_P (loc_note))
26413 newloc->label = last_label;
26414 else
26415 {
26416 if (!last_postcall_label)
26417 {
26418 sprintf (loclabel, "%s-1", last_label);
26419 last_postcall_label = ggc_strdup (loclabel);
26420 }
26421 newloc->label = last_postcall_label;
26422 }
26423
26424 last_var_location_insn = next_real;
26425 last_in_cold_section_p = in_cold_section_p;
26426}
26427
26428/* Called from finalize_size_functions for size functions so that their body
26429 can be encoded in the debug info to describe the layout of variable-length
26430 structures. */
26431
26432static void
26433dwarf2out_size_function (tree decl)
26434{
26435 function_to_dwarf_procedure (decl);
26436}
26437
26438/* Note in one location list that text section has changed. */
26439
26440int
26441var_location_switch_text_section_1 (var_loc_list **slot, void *)
26442{
26443 var_loc_list *list = *slot;
26444 if (list->first)
26445 list->last_before_switch
26446 = list->last->next ? list->last->next : list->last;
26447 return 1;
26448}
26449
26450/* Note in all location lists that text section has changed. */
26451
26452static void
26453var_location_switch_text_section (void)
26454{
26455 if (decl_loc_table == NULL)
26456 return;
26457
26458 decl_loc_table->traverse<void *, var_location_switch_text_section_1> (NULL);
26459}
26460
26461/* Create a new line number table. */
26462
26463static dw_line_info_table *
26464new_line_info_table (void)
26465{
26466 dw_line_info_table *table;
26467
26468 table = ggc_cleared_alloc<dw_line_info_table> ();
26469 table->file_num = 1;
26470 table->line_num = 1;
26471 table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
26472
26473 return table;
26474}
26475
26476/* Lookup the "current" table into which we emit line info, so
26477 that we don't have to do it for every source line. */
26478
26479static void
26480set_cur_line_info_table (section *sec)
26481{
26482 dw_line_info_table *table;
26483
26484 if (sec == text_section)
26485 table = text_section_line_info;
26486 else if (sec == cold_text_section)
26487 {
26488 table = cold_text_section_line_info;
26489 if (!table)
26490 {
26491 cold_text_section_line_info = table = new_line_info_table ();
26492 table->end_label = cold_end_label;
26493 }
26494 }
26495 else
26496 {
26497 const char *end_label;
26498
26499 if (flag_reorder_blocks_and_partition)
26500 {
26501 if (in_cold_section_p)
26502 end_label = crtl->subsections.cold_section_end_label;
26503 else
26504 end_label = crtl->subsections.hot_section_end_label;
26505 }
26506 else
26507 {
26508 char label[MAX_ARTIFICIAL_LABEL_BYTES];
26509 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
26510 current_function_funcdef_no);
26511 end_label = ggc_strdup (label);
26512 }
26513
26514 table = new_line_info_table ();
26515 table->end_label = end_label;
26516
26517 vec_safe_push (separate_line_info, table);
26518 }
26519
26520 if (DWARF2_ASM_LINE_DEBUG_INFO)
26521 table->is_stmt = (cur_line_info_table
26522 ? cur_line_info_table->is_stmt
26523 : DWARF_LINE_DEFAULT_IS_STMT_START);
26524 cur_line_info_table = table;
26525}
26526
26527
26528/* We need to reset the locations at the beginning of each
26529 function. We can't do this in the end_function hook, because the
26530 declarations that use the locations won't have been output when
26531 that hook is called. Also compute have_multiple_function_sections here. */
26532
26533static void
26534dwarf2out_begin_function (tree fun)
26535{
26536 section *sec = function_section (fun);
26537
26538 if (sec != text_section)
26539 have_multiple_function_sections = true;
26540
26541 if (flag_reorder_blocks_and_partition && !cold_text_section)
26542 {
26543 gcc_assert (current_function_decl == fun);
26544 cold_text_section = unlikely_text_section ();
26545 switch_to_section (cold_text_section);
26546 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
26547 switch_to_section (sec);
26548 }
26549
26550 dwarf2out_note_section_used ();
26551 call_site_count = 0;
26552 tail_call_site_count = 0;
26553
26554 set_cur_line_info_table (sec);
26555}
26556
26557/* Helper function of dwarf2out_end_function, called only after emitting
26558 the very first function into assembly. Check if some .debug_loc range
26559 might end with a .LVL* label that could be equal to .Ltext0.
26560 In that case we must force using absolute addresses in .debug_loc ranges,
26561 because this range could be .LVLN-.Ltext0 .. .LVLM-.Ltext0 for
26562 .LVLN == .LVLM == .Ltext0, thus 0 .. 0, which is a .debug_loc
26563 list terminator.
26564 Set have_multiple_function_sections to true in that case and
26565 terminate htab traversal. */
26566
26567int
26568find_empty_loc_ranges_at_text_label (var_loc_list **slot, int)
26569{
26570 var_loc_list *entry = *slot;
26571 struct var_loc_node *node;
26572
26573 node = entry->first;
26574 if (node && node->next && node->next->label)
26575 {
26576 unsigned int i;
26577 const char *label = node->next->label;
26578 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
26579
26580 for (i = 0; i < first_loclabel_num_not_at_text_label; i++)
26581 {
26582 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", i);
26583 if (strcmp (label, loclabel) == 0)
26584 {
26585 have_multiple_function_sections = true;
26586 return 0;
26587 }
26588 }
26589 }
26590 return 1;
26591}
26592
26593/* Hook called after emitting a function into assembly.
26594 This does something only for the very first function emitted. */
26595
26596static void
26597dwarf2out_end_function (unsigned int)
26598{
26599 if (in_first_function_p
26600 && !have_multiple_function_sections
26601 && first_loclabel_num_not_at_text_label
26602 && decl_loc_table)
26603 decl_loc_table->traverse<int, find_empty_loc_ranges_at_text_label> (0);
26604 in_first_function_p = false;
26605 maybe_at_text_label_p = false;
26606}
26607
26608/* Temporary holder for dwarf2out_register_main_translation_unit. Used to let
26609 front-ends register a translation unit even before dwarf2out_init is
26610 called. */
26611static tree main_translation_unit = NULL_TREE;
26612
26613/* Hook called by front-ends after they built their main translation unit.
26614 Associate comp_unit_die to UNIT. */
26615
26616static void
26617dwarf2out_register_main_translation_unit (tree unit)
26618{
26619 gcc_assert (TREE_CODE (unit) == TRANSLATION_UNIT_DECL
26620 && main_translation_unit == NULL_TREE);
26621 main_translation_unit = unit;
26622 /* If dwarf2out_init has not been called yet, it will perform the association
26623 itself looking at main_translation_unit. */
26624 if (decl_die_table != NULL)
26625 equate_decl_number_to_die (unit, comp_unit_die ());
26626}
26627
26628/* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE. */
26629
26630static void
26631push_dw_line_info_entry (dw_line_info_table *table,
26632 enum dw_line_info_opcode opcode, unsigned int val)
26633{
26634 dw_line_info_entry e;
26635 e.opcode = opcode;
26636 e.val = val;
26637 vec_safe_push (table->entries, e);
26638}
26639
26640/* Output a label to mark the beginning of a source code line entry
26641 and record information relating to this source line, in
26642 'line_info_table' for later output of the .debug_line section. */
26643/* ??? The discriminator parameter ought to be unsigned. */
26644
26645static void
26646dwarf2out_source_line (unsigned int line, unsigned int column,
26647 const char *filename,
26648 int discriminator, bool is_stmt)
26649{
26650 unsigned int file_num;
26651 dw_line_info_table *table;
26652
26653 if (debug_info_level < DINFO_LEVEL_TERSE || line == 0)
26654 return;
26655
26656 /* The discriminator column was added in dwarf4. Simplify the below
26657 by simply removing it if we're not supposed to output it. */
26658 if (dwarf_version < 4 && dwarf_strict)
26659 discriminator = 0;
26660
26661 if (!debug_column_info)
26662 column = 0;
26663
26664 table = cur_line_info_table;
26665 file_num = maybe_emit_file (lookup_filename (filename));
26666
26667 /* ??? TODO: Elide duplicate line number entries. Traditionally,
26668 the debugger has used the second (possibly duplicate) line number
26669 at the beginning of the function to mark the end of the prologue.
26670 We could eliminate any other duplicates within the function. For
26671 Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
26672 that second line number entry. */
26673 /* Recall that this end-of-prologue indication is *not* the same thing
26674 as the end_prologue debug hook. The NOTE_INSN_PROLOGUE_END note,
26675 to which the hook corresponds, follows the last insn that was
26676 emitted by gen_prologue. What we need is to precede the first insn
26677 that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
26678 insn that corresponds to something the user wrote. These may be
26679 very different locations once scheduling is enabled. */
26680
26681 if (0 && file_num == table->file_num
26682 && line == table->line_num
26683 && column == table->column_num
26684 && discriminator == table->discrim_num
26685 && is_stmt == table->is_stmt)
26686 return;
26687
26688 switch_to_section (current_function_section ());
26689
26690 /* If requested, emit something human-readable. */
26691 if (flag_debug_asm)
26692 {
26693 if (debug_column_info)
26694 fprintf (asm_out_file, "\t%s %s:%d:%d\n", ASM_COMMENT_START,
26695 filename, line, column);
26696 else
26697 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
26698 filename, line);
26699 }
26700
26701 if (DWARF2_ASM_LINE_DEBUG_INFO)
26702 {
26703 /* Emit the .loc directive understood by GNU as. */
26704 /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
26705 file_num, line, is_stmt, discriminator */
26706 fputs ("\t.loc ", asm_out_file);
26707 fprint_ul (asm_out_file, file_num);
26708 putc (' ', asm_out_file);
26709 fprint_ul (asm_out_file, line);
26710 putc (' ', asm_out_file);
26711 if (debug_column_info)
26712 fprint_ul (asm_out_file, column);
26713 else
26714 putc ('0', asm_out_file);
26715
26716 if (is_stmt != table->is_stmt)
26717 {
26718 fputs (" is_stmt ", asm_out_file);
26719 putc (is_stmt ? '1' : '0', asm_out_file);
26720 }
26721 if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
26722 {
26723 gcc_assert (discriminator > 0);
26724 fputs (" discriminator ", asm_out_file);
26725 fprint_ul (asm_out_file, (unsigned long) discriminator);
26726 }
26727 putc ('\n', asm_out_file);
26728 }
26729 else
26730 {
26731 unsigned int label_num = ++line_info_label_num;
26732
26733 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
26734
26735 push_dw_line_info_entry (table, LI_set_address, label_num);
26736 if (file_num != table->file_num)
26737 push_dw_line_info_entry (table, LI_set_file, file_num);
26738 if (discriminator != table->discrim_num)
26739 push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
26740 if (is_stmt != table->is_stmt)
26741 push_dw_line_info_entry (table, LI_negate_stmt, 0);
26742 push_dw_line_info_entry (table, LI_set_line, line);
26743 if (debug_column_info)
26744 push_dw_line_info_entry (table, LI_set_column, column);
26745 }
26746
26747 table->file_num = file_num;
26748 table->line_num = line;
26749 table->column_num = column;
26750 table->discrim_num = discriminator;
26751 table->is_stmt = is_stmt;
26752 table->in_use = true;
26753}
26754
26755/* Record the beginning of a new source file. */
26756
26757static void
26758dwarf2out_start_source_file (unsigned int lineno, const char *filename)
26759{
26760 if (flag_eliminate_dwarf2_dups)
26761 {
26762 /* Record the beginning of the file for break_out_includes. */
26763 dw_die_ref bincl_die;
26764
26765 bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die (), NULL);
26766 add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
26767 }
26768
26769 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
26770 {
26771 macinfo_entry e;
26772 e.code = DW_MACINFO_start_file;
26773 e.lineno = lineno;
26774 e.info = ggc_strdup (filename);
26775 vec_safe_push (macinfo_table, e);
26776 }
26777}
26778
26779/* Record the end of a source file. */
26780
26781static void
26782dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
26783{
26784 if (flag_eliminate_dwarf2_dups)
26785 /* Record the end of the file for break_out_includes. */
26786 new_die (DW_TAG_GNU_EINCL, comp_unit_die (), NULL);
26787
26788 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
26789 {
26790 macinfo_entry e;
26791 e.code = DW_MACINFO_end_file;
26792 e.lineno = lineno;
26793 e.info = NULL;
26794 vec_safe_push (macinfo_table, e);
26795 }
26796}
26797
26798/* Called from debug_define in toplev.c. The `buffer' parameter contains
26799 the tail part of the directive line, i.e. the part which is past the
26800 initial whitespace, #, whitespace, directive-name, whitespace part. */
26801
26802static void
26803dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
26804 const char *buffer ATTRIBUTE_UNUSED)
26805{
26806 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
26807 {
26808 macinfo_entry e;
26809 /* Insert a dummy first entry to be able to optimize the whole
26810 predefined macro block using DW_MACRO_import. */
26811 if (macinfo_table->is_empty () && lineno <= 1)
26812 {
26813 e.code = 0;
26814 e.lineno = 0;
26815 e.info = NULL;
26816 vec_safe_push (macinfo_table, e);
26817 }
26818 e.code = DW_MACINFO_define;
26819 e.lineno = lineno;
26820 e.info = ggc_strdup (buffer);
26821 vec_safe_push (macinfo_table, e);
26822 }
26823}
26824
26825/* Called from debug_undef in toplev.c. The `buffer' parameter contains
26826 the tail part of the directive line, i.e. the part which is past the
26827 initial whitespace, #, whitespace, directive-name, whitespace part. */
26828
26829static void
26830dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
26831 const char *buffer ATTRIBUTE_UNUSED)
26832{
26833 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
26834 {
26835 macinfo_entry e;
26836 /* Insert a dummy first entry to be able to optimize the whole
26837 predefined macro block using DW_MACRO_import. */
26838 if (macinfo_table->is_empty () && lineno <= 1)
26839 {
26840 e.code = 0;
26841 e.lineno = 0;
26842 e.info = NULL;
26843 vec_safe_push (macinfo_table, e);
26844 }
26845 e.code = DW_MACINFO_undef;
26846 e.lineno = lineno;
26847 e.info = ggc_strdup (buffer);
26848 vec_safe_push (macinfo_table, e);
26849 }
26850}
26851
26852/* Helpers to manipulate hash table of CUs. */
26853
26854struct macinfo_entry_hasher : nofree_ptr_hash <macinfo_entry>
26855{
26856 static inline hashval_t hash (const macinfo_entry *);
26857 static inline bool equal (const macinfo_entry *, const macinfo_entry *);
26858};
26859
26860inline hashval_t
26861macinfo_entry_hasher::hash (const macinfo_entry *entry)
26862{
26863 return htab_hash_string (entry->info);
26864}
26865
26866inline bool
26867macinfo_entry_hasher::equal (const macinfo_entry *entry1,
26868 const macinfo_entry *entry2)
26869{
26870 return !strcmp (entry1->info, entry2->info);
26871}
26872
26873typedef hash_table<macinfo_entry_hasher> macinfo_hash_type;
26874
26875/* Output a single .debug_macinfo entry. */
26876
26877static void
26878output_macinfo_op (macinfo_entry *ref)
26879{
26880 int file_num;
26881 size_t len;
26882 struct indirect_string_node *node;
26883 char label[MAX_ARTIFICIAL_LABEL_BYTES];
26884 struct dwarf_file_data *fd;
26885
26886 switch (ref->code)
26887 {
26888 case DW_MACINFO_start_file:
26889 fd = lookup_filename (ref->info);
26890 file_num = maybe_emit_file (fd);
26891 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
26892 dw2_asm_output_data_uleb128 (ref->lineno,
26893 "Included from line number %lu",
26894 (unsigned long) ref->lineno);
26895 dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
26896 break;
26897 case DW_MACINFO_end_file:
26898 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
26899 break;
26900 case DW_MACINFO_define:
26901 case DW_MACINFO_undef:
26902 len = strlen (ref->info) + 1;
26903 if (!dwarf_strict
26904 && len > DWARF_OFFSET_SIZE
26905 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
26906 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
26907 {
26908 ref->code = ref->code == DW_MACINFO_define
26909 ? DW_MACRO_define_strp : DW_MACRO_undef_strp;
26910 output_macinfo_op (ref);
26911 return;
26912 }
26913 dw2_asm_output_data (1, ref->code,
26914 ref->code == DW_MACINFO_define
26915 ? "Define macro" : "Undefine macro");
26916 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
26917 (unsigned long) ref->lineno);
26918 dw2_asm_output_nstring (ref->info, -1, "The macro");
26919 break;
26920 case DW_MACRO_define_strp:
26921 case DW_MACRO_undef_strp:
26922 node = find_AT_string (ref->info);
26923 gcc_assert (node
26924 && (node->form == DW_FORM_strp
26925 || node->form == DW_FORM_GNU_str_index));
26926 dw2_asm_output_data (1, ref->code,
26927 ref->code == DW_MACRO_define_strp
26928 ? "Define macro strp"
26929 : "Undefine macro strp");
26930 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
26931 (unsigned long) ref->lineno);
26932 if (node->form == DW_FORM_strp)
26933 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
26934 debug_str_section, "The macro: \"%s\"",
26935 ref->info);
26936 else
26937 dw2_asm_output_data_uleb128 (node->index, "The macro: \"%s\"",
26938 ref->info);
26939 break;
26940 case DW_MACRO_import:
26941 dw2_asm_output_data (1, ref->code, "Import");
26942 ASM_GENERATE_INTERNAL_LABEL (label,
26943 DEBUG_MACRO_SECTION_LABEL, ref->lineno);
26944 dw2_asm_output_offset (DWARF_OFFSET_SIZE, label, NULL, NULL);
26945 break;
26946 default:
26947 fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
26948 ASM_COMMENT_START, (unsigned long) ref->code);
26949 break;
26950 }
26951}
26952
26953/* Attempt to make a sequence of define/undef macinfo ops shareable with
26954 other compilation unit .debug_macinfo sections. IDX is the first
26955 index of a define/undef, return the number of ops that should be
26956 emitted in a comdat .debug_macinfo section and emit
26957 a DW_MACRO_import entry referencing it.
26958 If the define/undef entry should be emitted normally, return 0. */
26959
26960static unsigned
26961optimize_macinfo_range (unsigned int idx, vec<macinfo_entry, va_gc> *files,
26962 macinfo_hash_type **macinfo_htab)
26963{
26964 macinfo_entry *first, *second, *cur, *inc;
26965 char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
26966 unsigned char checksum[16];
26967 struct md5_ctx ctx;
26968 char *grp_name, *tail;
26969 const char *base;
26970 unsigned int i, count, encoded_filename_len, linebuf_len;
26971 macinfo_entry **slot;
26972
26973 first = &(*macinfo_table)[idx];
26974 second = &(*macinfo_table)[idx + 1];
26975
26976 /* Optimize only if there are at least two consecutive define/undef ops,
26977 and either all of them are before first DW_MACINFO_start_file
26978 with lineno {0,1} (i.e. predefined macro block), or all of them are
26979 in some included header file. */
26980 if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
26981 return 0;
26982 if (vec_safe_is_empty (files))
26983 {
26984 if (first->lineno > 1 || second->lineno > 1)
26985 return 0;
26986 }
26987 else if (first->lineno == 0)
26988 return 0;
26989
26990 /* Find the last define/undef entry that can be grouped together
26991 with first and at the same time compute md5 checksum of their
26992 codes, linenumbers and strings. */
26993 md5_init_ctx (&ctx);
26994 for (i = idx; macinfo_table->iterate (i, &cur); i++)
26995 if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
26996 break;
26997 else if (vec_safe_is_empty (files) && cur->lineno > 1)
26998 break;
26999 else
27000 {
27001 unsigned char code = cur->code;
27002 md5_process_bytes (&code, 1, &ctx);
27003 checksum_uleb128 (cur->lineno, &ctx);
27004 md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
27005 }
27006 md5_finish_ctx (&ctx, checksum);
27007 count = i - idx;
27008
27009 /* From the containing include filename (if any) pick up just
27010 usable characters from its basename. */
27011 if (vec_safe_is_empty (files))
27012 base = "";
27013 else
27014 base = lbasename (files->last ().info);
27015 for (encoded_filename_len = 0, i = 0; base[i]; i++)
27016 if (ISIDNUM (base[i]) || base[i] == '.')
27017 encoded_filename_len++;
27018 /* Count . at the end. */
27019 if (encoded_filename_len)
27020 encoded_filename_len++;
27021
27022 sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
27023 linebuf_len = strlen (linebuf);
27024
27025 /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum> */
27026 grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
27027 + 16 * 2 + 1);
27028 memcpy (grp_name, DWARF_OFFSET_SIZE == 4 ? "wm4." : "wm8.", 4);
27029 tail = grp_name + 4;
27030 if (encoded_filename_len)
27031 {
27032 for (i = 0; base[i]; i++)
27033 if (ISIDNUM (base[i]) || base[i] == '.')
27034 *tail++ = base[i];
27035 *tail++ = '.';
27036 }
27037 memcpy (tail, linebuf, linebuf_len);
27038 tail += linebuf_len;
27039 *tail++ = '.';
27040 for (i = 0; i < 16; i++)
27041 sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
27042
27043 /* Construct a macinfo_entry for DW_MACRO_import
27044 in the empty vector entry before the first define/undef. */
27045 inc = &(*macinfo_table)[idx - 1];
27046 inc->code = DW_MACRO_import;
27047 inc->lineno = 0;
27048 inc->info = ggc_strdup (grp_name);
27049 if (!*macinfo_htab)
27050 *macinfo_htab = new macinfo_hash_type (10);
27051 /* Avoid emitting duplicates. */
27052 slot = (*macinfo_htab)->find_slot (inc, INSERT);
27053 if (*slot != NULL)
27054 {
27055 inc->code = 0;
27056 inc->info = NULL;
27057 /* If such an entry has been used before, just emit
27058 a DW_MACRO_import op. */
27059 inc = *slot;
27060 output_macinfo_op (inc);
27061 /* And clear all macinfo_entry in the range to avoid emitting them
27062 in the second pass. */
27063 for (i = idx; macinfo_table->iterate (i, &cur) && i < idx + count; i++)
27064 {
27065 cur->code = 0;
27066 cur->info = NULL;
27067 }
27068 }
27069 else
27070 {
27071 *slot = inc;
27072 inc->lineno = (*macinfo_htab)->elements ();
27073 output_macinfo_op (inc);
27074 }
27075 return count;
27076}
27077
27078/* Save any strings needed by the macinfo table in the debug str
27079 table. All strings must be collected into the table by the time
27080 index_string is called. */
27081
27082static void
27083save_macinfo_strings (void)
27084{
27085 unsigned len;
27086 unsigned i;
27087 macinfo_entry *ref;
27088
27089 for (i = 0; macinfo_table && macinfo_table->iterate (i, &ref); i++)
27090 {
27091 switch (ref->code)
27092 {
27093 /* Match the logic in output_macinfo_op to decide on
27094 indirect strings. */
27095 case DW_MACINFO_define:
27096 case DW_MACINFO_undef:
27097 len = strlen (ref->info) + 1;
27098 if (!dwarf_strict
27099 && len > DWARF_OFFSET_SIZE
27100 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
27101 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
27102 set_indirect_string (find_AT_string (ref->info));
27103 break;
27104 case DW_MACRO_define_strp:
27105 case DW_MACRO_undef_strp:
27106 set_indirect_string (find_AT_string (ref->info));
27107 break;
27108 default:
27109 break;
27110 }
27111 }
27112}
27113
27114/* Output macinfo section(s). */
27115
27116static void
27117output_macinfo (void)
27118{
27119 unsigned i;
27120 unsigned long length = vec_safe_length (macinfo_table);
27121 macinfo_entry *ref;
27122 vec<macinfo_entry, va_gc> *files = NULL;
27123 macinfo_hash_type *macinfo_htab = NULL;
27124 char dl_section_ref[MAX_ARTIFICIAL_LABEL_BYTES];
27125
27126 if (! length)
27127 return;
27128
27129 /* output_macinfo* uses these interchangeably. */
27130 gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_define
27131 && (int) DW_MACINFO_undef == (int) DW_MACRO_undef
27132 && (int) DW_MACINFO_start_file == (int) DW_MACRO_start_file
27133 && (int) DW_MACINFO_end_file == (int) DW_MACRO_end_file);
27134
27135 /* AIX Assembler inserts the length, so adjust the reference to match the
27136 offset expected by debuggers. */
27137 strcpy (dl_section_ref, debug_line_section_label);
27138 if (XCOFF_DEBUGGING_INFO)
27139 strcat (dl_section_ref, DWARF_INITIAL_LENGTH_SIZE_STR);
27140
27141 /* For .debug_macro emit the section header. */
27142 if (!dwarf_strict || dwarf_version >= 5)
27143 {
27144 dw2_asm_output_data (2, dwarf_version >= 5 ? 5 : 4,
27145 "DWARF macro version number");
27146 if (DWARF_OFFSET_SIZE == 8)
27147 dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
27148 else
27149 dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
27150 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
27151 (!dwarf_split_debug_info ? dl_section_ref
27152 : debug_skeleton_line_section_label),
27153 debug_line_section, NULL);
27154 }
27155
27156 /* In the first loop, it emits the primary .debug_macinfo section
27157 and after each emitted op the macinfo_entry is cleared.
27158 If a longer range of define/undef ops can be optimized using
27159 DW_MACRO_import, the DW_MACRO_import op is emitted and kept in
27160 the vector before the first define/undef in the range and the
27161 whole range of define/undef ops is not emitted and kept. */
27162 for (i = 0; macinfo_table->iterate (i, &ref); i++)
27163 {
27164 switch (ref->code)
27165 {
27166 case DW_MACINFO_start_file:
27167 vec_safe_push (files, *ref);
27168 break;
27169 case DW_MACINFO_end_file:
27170 if (!vec_safe_is_empty (files))
27171 files->pop ();
27172 break;
27173 case DW_MACINFO_define:
27174 case DW_MACINFO_undef:
27175 if ((!dwarf_strict || dwarf_version >= 5)
27176 && HAVE_COMDAT_GROUP
27177 && vec_safe_length (files) != 1
27178 && i > 0
27179 && i + 1 < length
27180 && (*macinfo_table)[i - 1].code == 0)
27181 {
27182 unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
27183 if (count)
27184 {
27185 i += count - 1;
27186 continue;
27187 }
27188 }
27189 break;
27190 case 0:
27191 /* A dummy entry may be inserted at the beginning to be able
27192 to optimize the whole block of predefined macros. */
27193 if (i == 0)
27194 continue;
27195 default:
27196 break;
27197 }
27198 output_macinfo_op (ref);
27199 ref->info = NULL;
27200 ref->code = 0;
27201 }
27202
27203 if (!macinfo_htab)
27204 return;
27205
27206 delete macinfo_htab;
27207 macinfo_htab = NULL;
27208
27209 /* If any DW_MACRO_import were used, on those DW_MACRO_import entries
27210 terminate the current chain and switch to a new comdat .debug_macinfo
27211 section and emit the define/undef entries within it. */
27212 for (i = 0; macinfo_table->iterate (i, &ref); i++)
27213 switch (ref->code)
27214 {
27215 case 0:
27216 continue;
27217 case DW_MACRO_import:
27218 {
27219 char label[MAX_ARTIFICIAL_LABEL_BYTES];
27220 tree comdat_key = get_identifier (ref->info);
27221 /* Terminate the previous .debug_macinfo section. */
27222 dw2_asm_output_data (1, 0, "End compilation unit");
27223 targetm.asm_out.named_section (debug_macinfo_section_name,
27224 SECTION_DEBUG
27225 | SECTION_LINKONCE,
27226 comdat_key);
27227 ASM_GENERATE_INTERNAL_LABEL (label,
27228 DEBUG_MACRO_SECTION_LABEL,
27229 ref->lineno);
27230 ASM_OUTPUT_LABEL (asm_out_file, label);
27231 ref->code = 0;
27232 ref->info = NULL;
27233 dw2_asm_output_data (2, dwarf_version >= 5 ? 5 : 4,
27234 "DWARF macro version number");
27235 if (DWARF_OFFSET_SIZE == 8)
27236 dw2_asm_output_data (1, 1, "Flags: 64-bit");
27237 else
27238 dw2_asm_output_data (1, 0, "Flags: 32-bit");
27239 }
27240 break;
27241 case DW_MACINFO_define:
27242 case DW_MACINFO_undef:
27243 output_macinfo_op (ref);
27244 ref->code = 0;
27245 ref->info = NULL;
27246 break;
27247 default:
27248 gcc_unreachable ();
27249 }
27250}
27251
27252/* Initialize the various sections and labels for dwarf output. */
27253
27254static void
27255init_sections_and_labels (void)
27256{
27257 if (!dwarf_split_debug_info)
27258 {
27259 debug_info_section = get_section (DEBUG_INFO_SECTION,
27260 SECTION_DEBUG, NULL);
27261 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
27262 SECTION_DEBUG, NULL);
27263 debug_loc_section = get_section (dwarf_version >= 5
27264 ? DEBUG_LOCLISTS_SECTION
27265 : DEBUG_LOC_SECTION,
27266 SECTION_DEBUG, NULL);
27267 debug_macinfo_section_name
27268 = (dwarf_strict && dwarf_version < 5)
27269 ? DEBUG_MACINFO_SECTION : DEBUG_MACRO_SECTION;
27270 debug_macinfo_section = get_section (debug_macinfo_section_name,
27271 SECTION_DEBUG, NULL);
27272 }
27273 else
27274 {
27275 debug_info_section = get_section (DEBUG_DWO_INFO_SECTION,
27276 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
27277 debug_abbrev_section = get_section (DEBUG_DWO_ABBREV_SECTION,
27278 SECTION_DEBUG | SECTION_EXCLUDE,
27279 NULL);
27280 debug_addr_section = get_section (DEBUG_ADDR_SECTION,
27281 SECTION_DEBUG, NULL);
27282 debug_skeleton_info_section = get_section (DEBUG_INFO_SECTION,
27283 SECTION_DEBUG, NULL);
27284 debug_skeleton_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
27285 SECTION_DEBUG, NULL);
27286 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
27287 DEBUG_SKELETON_ABBREV_SECTION_LABEL, 0);
27288
27289 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections stay in
27290 the main .o, but the skeleton_line goes into the split off dwo. */
27291 debug_skeleton_line_section
27292 = get_section (DEBUG_DWO_LINE_SECTION,
27293 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
27294 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
27295 DEBUG_SKELETON_LINE_SECTION_LABEL, 0);
27296 debug_str_offsets_section = get_section (DEBUG_DWO_STR_OFFSETS_SECTION,
27297 SECTION_DEBUG | SECTION_EXCLUDE,
27298 NULL);
27299 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
27300 DEBUG_SKELETON_INFO_SECTION_LABEL, 0);
27301 debug_loc_section = get_section (dwarf_version >= 5
27302 ? DEBUG_DWO_LOCLISTS_SECTION
27303 : DEBUG_DWO_LOC_SECTION,
27304 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
27305 debug_str_dwo_section = get_section (DEBUG_STR_DWO_SECTION,
27306 DEBUG_STR_DWO_SECTION_FLAGS, NULL);
27307 debug_macinfo_section_name
27308 = (dwarf_strict && dwarf_version < 5)
27309 ? DEBUG_DWO_MACINFO_SECTION : DEBUG_DWO_MACRO_SECTION;
27310 debug_macinfo_section = get_section (debug_macinfo_section_name,
27311 SECTION_DEBUG | SECTION_EXCLUDE,
27312 NULL);
27313 }
27314 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
27315 SECTION_DEBUG, NULL);
27316 debug_line_section = get_section (DEBUG_LINE_SECTION,
27317 SECTION_DEBUG, NULL);
27318 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
27319 SECTION_DEBUG, NULL);
27320 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
27321 SECTION_DEBUG, NULL);
27322 debug_str_section = get_section (DEBUG_STR_SECTION,
27323 DEBUG_STR_SECTION_FLAGS, NULL);
27324 if (!dwarf_split_debug_info && !DWARF2_ASM_LINE_DEBUG_INFO)
27325 debug_line_str_section = get_section (DEBUG_LINE_STR_SECTION,
27326 DEBUG_STR_SECTION_FLAGS, NULL);
27327
27328 debug_ranges_section = get_section (dwarf_version >= 5
27329 ? DEBUG_RNGLISTS_SECTION
27330 : DEBUG_RANGES_SECTION,
27331 SECTION_DEBUG, NULL);
27332 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
27333 SECTION_DEBUG, NULL);
27334
27335 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
27336 DEBUG_ABBREV_SECTION_LABEL, 0);
27337 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
27338 DEBUG_INFO_SECTION_LABEL, 0);
27339 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
27340 DEBUG_LINE_SECTION_LABEL, 0);
27341 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
27342 DEBUG_RANGES_SECTION_LABEL, 0);
27343 if (dwarf_version >= 5 && dwarf_split_debug_info)
27344 ASM_GENERATE_INTERNAL_LABEL (ranges_base_label,
27345 DEBUG_RANGES_SECTION_LABEL, 1);
27346 ASM_GENERATE_INTERNAL_LABEL (debug_addr_section_label,
27347 DEBUG_ADDR_SECTION_LABEL, 0);
27348 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
27349 (dwarf_strict && dwarf_version < 5)
27350 ? DEBUG_MACINFO_SECTION_LABEL
27351 : DEBUG_MACRO_SECTION_LABEL, 0);
27352 ASM_GENERATE_INTERNAL_LABEL (loc_section_label, DEBUG_LOC_SECTION_LABEL, 0);
27353}
27354
27355/* Set up for Dwarf output at the start of compilation. */
27356
27357static void
27358dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
27359{
27360 /* This option is currently broken, see (PR53118 and PR46102). */
27361 if (flag_eliminate_dwarf2_dups
27362 && strstr (lang_hooks.name, "C++"))
27363 {
27364 warning (0, "-feliminate-dwarf2-dups is broken for C++, ignoring");
27365 flag_eliminate_dwarf2_dups = 0;
27366 }
27367
27368 /* Allocate the file_table. */
27369 file_table = hash_table<dwarf_file_hasher>::create_ggc (50);
27370
27371#ifndef DWARF2_LINENO_DEBUGGING_INFO
27372 /* Allocate the decl_die_table. */
27373 decl_die_table = hash_table<decl_die_hasher>::create_ggc (10);
27374
27375 /* Allocate the decl_loc_table. */
27376 decl_loc_table = hash_table<decl_loc_hasher>::create_ggc (10);
27377
27378 /* Allocate the cached_dw_loc_list_table. */
27379 cached_dw_loc_list_table = hash_table<dw_loc_list_hasher>::create_ggc (10);
27380
27381 /* Allocate the initial hunk of the decl_scope_table. */
27382 vec_alloc (decl_scope_table, 256);
27383
27384 /* Allocate the initial hunk of the abbrev_die_table. */
27385 vec_alloc (abbrev_die_table, 256);
27386 /* Zero-th entry is allocated, but unused. */
27387 abbrev_die_table->quick_push (NULL);
27388
27389 /* Allocate the dwarf_proc_stack_usage_map. */
27390 dwarf_proc_stack_usage_map = new hash_map<dw_die_ref, int>;
27391
27392 /* Allocate the pubtypes and pubnames vectors. */
27393 vec_alloc (pubname_table, 32);
27394 vec_alloc (pubtype_table, 32);
27395
27396 vec_alloc (incomplete_types, 64);
27397
27398 vec_alloc (used_rtx_array, 32);
27399
27400 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
27401 vec_alloc (macinfo_table, 64);
27402#endif
27403
27404 /* If front-ends already registered a main translation unit but we were not
27405 ready to perform the association, do this now. */
27406 if (main_translation_unit != NULL_TREE)
27407 equate_decl_number_to_die (main_translation_unit, comp_unit_die ());
27408}
27409
27410/* Called before compile () starts outputtting functions, variables
27411 and toplevel asms into assembly. */
27412
27413static void
27414dwarf2out_assembly_start (void)
27415{
27416#ifndef DWARF2_LINENO_DEBUGGING_INFO
27417 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
27418 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
27419 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
27420 COLD_TEXT_SECTION_LABEL, 0);
27421 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
27422
27423 switch_to_section (text_section);
27424 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
27425#endif
27426
27427 /* Make sure the line number table for .text always exists. */
27428 text_section_line_info = new_line_info_table ();
27429 text_section_line_info->end_label = text_end_label;
27430
27431#ifdef DWARF2_LINENO_DEBUGGING_INFO
27432 cur_line_info_table = text_section_line_info;
27433#endif
27434
27435 if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
27436 && dwarf2out_do_cfi_asm ()
27437 && (!(flag_unwind_tables || flag_exceptions)
27438 || targetm_common.except_unwind_info (&global_options) != UI_DWARF2))
27439 fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
27440}
27441
27442/* A helper function for dwarf2out_finish called through
27443 htab_traverse. Assign a string its index. All strings must be
27444 collected into the table by the time index_string is called,
27445 because the indexing code relies on htab_traverse to traverse nodes
27446 in the same order for each run. */
27447
27448int
27449index_string (indirect_string_node **h, unsigned int *index)
27450{
27451 indirect_string_node *node = *h;
27452
27453 find_string_form (node);
27454 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
27455 {
27456 gcc_assert (node->index == NO_INDEX_ASSIGNED);
27457 node->index = *index;
27458 *index += 1;
27459 }
27460 return 1;
27461}
27462
27463/* A helper function for output_indirect_strings called through
27464 htab_traverse. Output the offset to a string and update the
27465 current offset. */
27466
27467int
27468output_index_string_offset (indirect_string_node **h, unsigned int *offset)
27469{
27470 indirect_string_node *node = *h;
27471
27472 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
27473 {
27474 /* Assert that this node has been assigned an index. */
27475 gcc_assert (node->index != NO_INDEX_ASSIGNED
27476 && node->index != NOT_INDEXED);
27477 dw2_asm_output_data (DWARF_OFFSET_SIZE, *offset,
27478 "indexed string 0x%x: %s", node->index, node->str);
27479 *offset += strlen (node->str) + 1;
27480 }
27481 return 1;
27482}
27483
27484/* A helper function for dwarf2out_finish called through
27485 htab_traverse. Output the indexed string. */
27486
27487int
27488output_index_string (indirect_string_node **h, unsigned int *cur_idx)
27489{
27490 struct indirect_string_node *node = *h;
27491
27492 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
27493 {
27494 /* Assert that the strings are output in the same order as their
27495 indexes were assigned. */
27496 gcc_assert (*cur_idx == node->index);
27497 assemble_string (node->str, strlen (node->str) + 1);
27498 *cur_idx += 1;
27499 }
27500 return 1;
27501}
27502
27503/* A helper function for dwarf2out_finish called through
27504 htab_traverse. Emit one queued .debug_str string. */
27505
27506int
27507output_indirect_string (indirect_string_node **h, enum dwarf_form form)
27508{
27509 struct indirect_string_node *node = *h;
27510
27511 node->form = find_string_form (node);
27512 if (node->form == form && node->refcount > 0)
27513 {
27514 ASM_OUTPUT_LABEL (asm_out_file, node->label);
27515 assemble_string (node->str, strlen (node->str) + 1);
27516 }
27517
27518 return 1;
27519}
27520
27521/* Output the indexed string table. */
27522
27523static void
27524output_indirect_strings (void)
27525{
27526 switch_to_section (debug_str_section);
27527 if (!dwarf_split_debug_info)
27528 debug_str_hash->traverse<enum dwarf_form,
27529 output_indirect_string> (DW_FORM_strp);
27530 else
27531 {
27532 unsigned int offset = 0;
27533 unsigned int cur_idx = 0;
27534
27535 skeleton_debug_str_hash->traverse<enum dwarf_form,
27536 output_indirect_string> (DW_FORM_strp);
27537
27538 switch_to_section (debug_str_offsets_section);
27539 debug_str_hash->traverse_noresize
27540 <unsigned int *, output_index_string_offset> (&offset);
27541 switch_to_section (debug_str_dwo_section);
27542 debug_str_hash->traverse_noresize<unsigned int *, output_index_string>
27543 (&cur_idx);
27544 }
27545}
27546
27547/* Callback for htab_traverse to assign an index to an entry in the
27548 table, and to write that entry to the .debug_addr section. */
27549
27550int
27551output_addr_table_entry (addr_table_entry **slot, unsigned int *cur_index)
27552{
27553 addr_table_entry *entry = *slot;
27554
27555 if (entry->refcount == 0)
27556 {
27557 gcc_assert (entry->index == NO_INDEX_ASSIGNED
27558 || entry->index == NOT_INDEXED);
27559 return 1;
27560 }
27561
27562 gcc_assert (entry->index == *cur_index);
27563 (*cur_index)++;
27564
27565 switch (entry->kind)
27566 {
27567 case ate_kind_rtx:
27568 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, entry->addr.rtl,
27569 "0x%x", entry->index);
27570 break;
27571 case ate_kind_rtx_dtprel:
27572 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
27573 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
27574 DWARF2_ADDR_SIZE,
27575 entry->addr.rtl);
27576 fputc ('\n', asm_out_file);
27577 break;
27578 case ate_kind_label:
27579 dw2_asm_output_addr (DWARF2_ADDR_SIZE, entry->addr.label,
27580 "0x%x", entry->index);
27581 break;
27582 default:
27583 gcc_unreachable ();
27584 }
27585 return 1;
27586}
27587
27588/* Produce the .debug_addr section. */
27589
27590static void
27591output_addr_table (void)
27592{
27593 unsigned int index = 0;
27594 if (addr_index_table == NULL || addr_index_table->size () == 0)
27595 return;
27596
27597 switch_to_section (debug_addr_section);
27598 addr_index_table
27599 ->traverse_noresize<unsigned int *, output_addr_table_entry> (&index);
27600}
27601
27602#if ENABLE_ASSERT_CHECKING
27603/* Verify that all marks are clear. */
27604
27605static void
27606verify_marks_clear (dw_die_ref die)
27607{
27608 dw_die_ref c;
27609
27610 gcc_assert (! die->die_mark);
27611 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
27612}
27613#endif /* ENABLE_ASSERT_CHECKING */
27614
27615/* Clear the marks for a die and its children.
27616 Be cool if the mark isn't set. */
27617
27618static void
27619prune_unmark_dies (dw_die_ref die)
27620{
27621 dw_die_ref c;
27622
27623 if (die->die_mark)
27624 die->die_mark = 0;
27625 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
27626}
27627
27628/* Given LOC that is referenced by a DIE we're marking as used, find all
27629 referenced DWARF procedures it references and mark them as used. */
27630
27631static void
27632prune_unused_types_walk_loc_descr (dw_loc_descr_ref loc)
27633{
27634 for (; loc != NULL; loc = loc->dw_loc_next)
27635 switch (loc->dw_loc_opc)
27636 {
27637 case DW_OP_implicit_pointer:
27638 case DW_OP_convert:
27639 case DW_OP_reinterpret:
27640 case DW_OP_GNU_implicit_pointer:
27641 case DW_OP_GNU_convert:
27642 case DW_OP_GNU_reinterpret:
27643 if (loc->dw_loc_oprnd1.val_class == dw_val_class_die_ref)
27644 prune_unused_types_mark (loc->dw_loc_oprnd1.v.val_die_ref.die, 1);
27645 break;
27646 case DW_OP_GNU_variable_value:
27647 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
27648 {
27649 dw_die_ref ref
27650 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
27651 if (ref == NULL)
27652 break;
27653 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
27654 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
27655 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
27656 }
27657 /* FALLTHRU */
27658 case DW_OP_call2:
27659 case DW_OP_call4:
27660 case DW_OP_call_ref:
27661 case DW_OP_const_type:
27662 case DW_OP_GNU_const_type:
27663 case DW_OP_GNU_parameter_ref:
27664 gcc_assert (loc->dw_loc_oprnd1.val_class == dw_val_class_die_ref);
27665 prune_unused_types_mark (loc->dw_loc_oprnd1.v.val_die_ref.die, 1);
27666 break;
27667 case DW_OP_regval_type:
27668 case DW_OP_deref_type:
27669 case DW_OP_GNU_regval_type:
27670 case DW_OP_GNU_deref_type:
27671 gcc_assert (loc->dw_loc_oprnd2.val_class == dw_val_class_die_ref);
27672 prune_unused_types_mark (loc->dw_loc_oprnd2.v.val_die_ref.die, 1);
27673 break;
27674 case DW_OP_entry_value:
27675 case DW_OP_GNU_entry_value:
27676 gcc_assert (loc->dw_loc_oprnd1.val_class == dw_val_class_loc);
27677 prune_unused_types_walk_loc_descr (loc->dw_loc_oprnd1.v.val_loc);
27678 break;
27679 default:
27680 break;
27681 }
27682}
27683
27684/* Given DIE that we're marking as used, find any other dies
27685 it references as attributes and mark them as used. */
27686
27687static void
27688prune_unused_types_walk_attribs (dw_die_ref die)
27689{
27690 dw_attr_node *a;
27691 unsigned ix;
27692
27693 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
27694 {
27695 switch (AT_class (a))
27696 {
27697 /* Make sure DWARF procedures referenced by location descriptions will
27698 get emitted. */
27699 case dw_val_class_loc:
27700 prune_unused_types_walk_loc_descr (AT_loc (a));
27701 break;
27702 case dw_val_class_loc_list:
27703 for (dw_loc_list_ref list = AT_loc_list (a);
27704 list != NULL;
27705 list = list->dw_loc_next)
27706 prune_unused_types_walk_loc_descr (list->expr);
27707 break;
27708
27709 case dw_val_class_die_ref:
27710 /* A reference to another DIE.
27711 Make sure that it will get emitted.
27712 If it was broken out into a comdat group, don't follow it. */
27713 if (! AT_ref (a)->comdat_type_p
27714 || a->dw_attr == DW_AT_specification)
27715 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
27716 break;
27717
27718 case dw_val_class_str:
27719 /* Set the string's refcount to 0 so that prune_unused_types_mark
27720 accounts properly for it. */
27721 a->dw_attr_val.v.val_str->refcount = 0;
27722 break;
27723
27724 default:
27725 break;
27726 }
27727 }
27728}
27729
27730/* Mark the generic parameters and arguments children DIEs of DIE. */
27731
27732static void
27733prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
27734{
27735 dw_die_ref c;
27736
27737 if (die == NULL || die->die_child == NULL)
27738 return;
27739 c = die->die_child;
27740 do
27741 {
27742 if (is_template_parameter (c))
27743 prune_unused_types_mark (c, 1);
27744 c = c->die_sib;
27745 } while (c && c != die->die_child);
27746}
27747
27748/* Mark DIE as being used. If DOKIDS is true, then walk down
27749 to DIE's children. */
27750
27751static void
27752prune_unused_types_mark (dw_die_ref die, int dokids)
27753{
27754 dw_die_ref c;
27755
27756 if (die->die_mark == 0)
27757 {
27758 /* We haven't done this node yet. Mark it as used. */
27759 die->die_mark = 1;
27760 /* If this is the DIE of a generic type instantiation,
27761 mark the children DIEs that describe its generic parms and
27762 args. */
27763 prune_unused_types_mark_generic_parms_dies (die);
27764
27765 /* We also have to mark its parents as used.
27766 (But we don't want to mark our parent's kids due to this,
27767 unless it is a class.) */
27768 if (die->die_parent)
27769 prune_unused_types_mark (die->die_parent,
27770 class_scope_p (die->die_parent));
27771
27772 /* Mark any referenced nodes. */
27773 prune_unused_types_walk_attribs (die);
27774
27775 /* If this node is a specification,
27776 also mark the definition, if it exists. */
27777 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
27778 prune_unused_types_mark (die->die_definition, 1);
27779 }
27780
27781 if (dokids && die->die_mark != 2)
27782 {
27783 /* We need to walk the children, but haven't done so yet.
27784 Remember that we've walked the kids. */
27785 die->die_mark = 2;
27786
27787 /* If this is an array type, we need to make sure our
27788 kids get marked, even if they're types. If we're
27789 breaking out types into comdat sections, do this
27790 for all type definitions. */
27791 if (die->die_tag == DW_TAG_array_type
27792 || (use_debug_types
27793 && is_type_die (die) && ! is_declaration_die (die)))
27794 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
27795 else
27796 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
27797 }
27798}
27799
27800/* For local classes, look if any static member functions were emitted
27801 and if so, mark them. */
27802
27803static void
27804prune_unused_types_walk_local_classes (dw_die_ref die)
27805{
27806 dw_die_ref c;
27807
27808 if (die->die_mark == 2)
27809 return;
27810
27811 switch (die->die_tag)
27812 {
27813 case DW_TAG_structure_type:
27814 case DW_TAG_union_type:
27815 case DW_TAG_class_type:
27816 break;
27817
27818 case DW_TAG_subprogram:
27819 if (!get_AT_flag (die, DW_AT_declaration)
27820 || die->die_definition != NULL)
27821 prune_unused_types_mark (die, 1);
27822 return;
27823
27824 default:
27825 return;
27826 }
27827
27828 /* Mark children. */
27829 FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
27830}
27831
27832/* Walk the tree DIE and mark types that we actually use. */
27833
27834static void
27835prune_unused_types_walk (dw_die_ref die)
27836{
27837 dw_die_ref c;
27838
27839 /* Don't do anything if this node is already marked and
27840 children have been marked as well. */
27841 if (die->die_mark == 2)
27842 return;
27843
27844 switch (die->die_tag)
27845 {
27846 case DW_TAG_structure_type:
27847 case DW_TAG_union_type:
27848 case DW_TAG_class_type:
27849 if (die->die_perennial_p)
27850 break;
27851
27852 for (c = die->die_parent; c; c = c->die_parent)
27853 if (c->die_tag == DW_TAG_subprogram)
27854 break;
27855
27856 /* Finding used static member functions inside of classes
27857 is needed just for local classes, because for other classes
27858 static member function DIEs with DW_AT_specification
27859 are emitted outside of the DW_TAG_*_type. If we ever change
27860 it, we'd need to call this even for non-local classes. */
27861 if (c)
27862 prune_unused_types_walk_local_classes (die);
27863
27864 /* It's a type node --- don't mark it. */
27865 return;
27866
27867 case DW_TAG_const_type:
27868 case DW_TAG_packed_type:
27869 case DW_TAG_pointer_type:
27870 case DW_TAG_reference_type:
27871 case DW_TAG_rvalue_reference_type:
27872 case DW_TAG_volatile_type:
27873 case DW_TAG_typedef:
27874 case DW_TAG_array_type:
27875 case DW_TAG_interface_type:
27876 case DW_TAG_friend:
27877 case DW_TAG_enumeration_type:
27878 case DW_TAG_subroutine_type:
27879 case DW_TAG_string_type:
27880 case DW_TAG_set_type:
27881 case DW_TAG_subrange_type:
27882 case DW_TAG_ptr_to_member_type:
27883 case DW_TAG_file_type:
27884 /* Type nodes are useful only when other DIEs reference them --- don't
27885 mark them. */
27886 /* FALLTHROUGH */
27887
27888 case DW_TAG_dwarf_procedure:
27889 /* Likewise for DWARF procedures. */
27890
27891 if (die->die_perennial_p)
27892 break;
27893
27894 return;
27895
27896 default:
27897 /* Mark everything else. */
27898 break;
27899 }
27900
27901 if (die->die_mark == 0)
27902 {
27903 die->die_mark = 1;
27904
27905 /* Now, mark any dies referenced from here. */
27906 prune_unused_types_walk_attribs (die);
27907 }
27908
27909 die->die_mark = 2;
27910
27911 /* Mark children. */
27912 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
27913}
27914
27915/* Increment the string counts on strings referred to from DIE's
27916 attributes. */
27917
27918static void
27919prune_unused_types_update_strings (dw_die_ref die)
27920{
27921 dw_attr_node *a;
27922 unsigned ix;
27923
27924 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
27925 if (AT_class (a) == dw_val_class_str)
27926 {
27927 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
27928 s->refcount++;
27929 /* Avoid unnecessarily putting strings that are used less than
27930 twice in the hash table. */
27931 if (s->refcount
27932 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
27933 {
27934 indirect_string_node **slot
27935 = debug_str_hash->find_slot_with_hash (s->str,
27936 htab_hash_string (s->str),
27937 INSERT);
27938 gcc_assert (*slot == NULL);
27939 *slot = s;
27940 }
27941 }
27942}
27943
27944/* Mark DIE and its children as removed. */
27945
27946static void
27947mark_removed (dw_die_ref die)
27948{
27949 dw_die_ref c;
27950 die->removed = true;
27951 FOR_EACH_CHILD (die, c, mark_removed (c));
27952}
27953
27954/* Remove from the tree DIE any dies that aren't marked. */
27955
27956static void
27957prune_unused_types_prune (dw_die_ref die)
27958{
27959 dw_die_ref c;
27960
27961 gcc_assert (die->die_mark);
27962 prune_unused_types_update_strings (die);
27963
27964 if (! die->die_child)
27965 return;
27966
27967 c = die->die_child;
27968 do {
27969 dw_die_ref prev = c, next;
27970 for (c = c->die_sib; ! c->die_mark; c = next)
27971 if (c == die->die_child)
27972 {
27973 /* No marked children between 'prev' and the end of the list. */
27974 if (prev == c)
27975 /* No marked children at all. */
27976 die->die_child = NULL;
27977 else
27978 {
27979 prev->die_sib = c->die_sib;
27980 die->die_child = prev;
27981 }
27982 c->die_sib = NULL;
27983 mark_removed (c);
27984 return;
27985 }
27986 else
27987 {
27988 next = c->die_sib;
27989 c->die_sib = NULL;
27990 mark_removed (c);
27991 }
27992
27993 if (c != prev->die_sib)
27994 prev->die_sib = c;
27995 prune_unused_types_prune (c);
27996 } while (c != die->die_child);
27997}
27998
27999/* Remove dies representing declarations that we never use. */
28000
28001static void
28002prune_unused_types (void)
28003{
28004 unsigned int i;
28005 limbo_die_node *node;
28006 comdat_type_node *ctnode;
28007 pubname_entry *pub;
28008 dw_die_ref base_type;
28009
28010#if ENABLE_ASSERT_CHECKING
28011 /* All the marks should already be clear. */
28012 verify_marks_clear (comp_unit_die ());
28013 for (node = limbo_die_list; node; node = node->next)
28014 verify_marks_clear (node->die);
28015 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
28016 verify_marks_clear (ctnode->root_die);
28017#endif /* ENABLE_ASSERT_CHECKING */
28018
28019 /* Mark types that are used in global variables. */
28020 premark_types_used_by_global_vars ();
28021
28022 /* Set the mark on nodes that are actually used. */
28023 prune_unused_types_walk (comp_unit_die ());
28024 for (node = limbo_die_list; node; node = node->next)
28025 prune_unused_types_walk (node->die);
28026 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
28027 {
28028 prune_unused_types_walk (ctnode->root_die);
28029 prune_unused_types_mark (ctnode->type_die, 1);
28030 }
28031
28032 /* Also set the mark on nodes referenced from the pubname_table. Enumerators
28033 are unusual in that they are pubnames that are the children of pubtypes.
28034 They should only be marked via their parent DW_TAG_enumeration_type die,
28035 not as roots in themselves. */
28036 FOR_EACH_VEC_ELT (*pubname_table, i, pub)
28037 if (pub->die->die_tag != DW_TAG_enumerator)
28038 prune_unused_types_mark (pub->die, 1);
28039 for (i = 0; base_types.iterate (i, &base_type); i++)
28040 prune_unused_types_mark (base_type, 1);
28041
28042 /* For -fvar-tracking-assignments, also set the mark on nodes that could be
28043 referenced by DW_TAG_call_site DW_AT_call_origin (i.e. direct call
28044 callees). */
28045 cgraph_node *cnode;
28046 FOR_EACH_FUNCTION (cnode)
28047 if (cnode->referred_to_p (false))
28048 {
28049 dw_die_ref die = lookup_decl_die (cnode->decl);
28050 if (die == NULL || die->die_mark)
28051 continue;
28052 for (cgraph_edge *e = cnode->callers; e; e = e->next_caller)
28053 if (e->caller != cnode
28054 && opt_for_fn (e->caller->decl, flag_var_tracking_assignments))
28055 {
28056 prune_unused_types_mark (die, 1);
28057 break;
28058 }
28059 }
28060
28061 if (debug_str_hash)
28062 debug_str_hash->empty ();
28063 if (skeleton_debug_str_hash)
28064 skeleton_debug_str_hash->empty ();
28065 prune_unused_types_prune (comp_unit_die ());
28066 for (limbo_die_node **pnode = &limbo_die_list; *pnode; )
28067 {
28068 node = *pnode;
28069 if (!node->die->die_mark)
28070 *pnode = node->next;
28071 else
28072 {
28073 prune_unused_types_prune (node->die);
28074 pnode = &node->next;
28075 }
28076 }
28077 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
28078 prune_unused_types_prune (ctnode->root_die);
28079
28080 /* Leave the marks clear. */
28081 prune_unmark_dies (comp_unit_die ());
28082 for (node = limbo_die_list; node; node = node->next)
28083 prune_unmark_dies (node->die);
28084 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
28085 prune_unmark_dies (ctnode->root_die);
28086}
28087
28088/* Helpers to manipulate hash table of comdat type units. */
28089
28090struct comdat_type_hasher : nofree_ptr_hash <comdat_type_node>
28091{
28092 static inline hashval_t hash (const comdat_type_node *);
28093 static inline bool equal (const comdat_type_node *, const comdat_type_node *);
28094};
28095
28096inline hashval_t
28097comdat_type_hasher::hash (const comdat_type_node *type_node)
28098{
28099 hashval_t h;
28100 memcpy (&h, type_node->signature, sizeof (h));
28101 return h;
28102}
28103
28104inline bool
28105comdat_type_hasher::equal (const comdat_type_node *type_node_1,
28106 const comdat_type_node *type_node_2)
28107{
28108 return (! memcmp (type_node_1->signature, type_node_2->signature,
28109 DWARF_TYPE_SIGNATURE_SIZE));
28110}
28111
28112/* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
28113 to the location it would have been added, should we know its
28114 DECL_ASSEMBLER_NAME when we added other attributes. This will
28115 probably improve compactness of debug info, removing equivalent
28116 abbrevs, and hide any differences caused by deferring the
28117 computation of the assembler name, triggered by e.g. PCH. */
28118
28119static inline void
28120move_linkage_attr (dw_die_ref die)
28121{
28122 unsigned ix = vec_safe_length (die->die_attr);
28123 dw_attr_node linkage = (*die->die_attr)[ix - 1];
28124
28125 gcc_assert (linkage.dw_attr == DW_AT_linkage_name
28126 || linkage.dw_attr == DW_AT_MIPS_linkage_name);
28127
28128 while (--ix > 0)
28129 {
28130 dw_attr_node *prev = &(*die->die_attr)[ix - 1];
28131
28132 if (prev->dw_attr == DW_AT_decl_line
28133 || prev->dw_attr == DW_AT_decl_column
28134 || prev->dw_attr == DW_AT_name)
28135 break;
28136 }
28137
28138 if (ix != vec_safe_length (die->die_attr) - 1)
28139 {
28140 die->die_attr->pop ();
28141 die->die_attr->quick_insert (ix, linkage);
28142 }
28143}
28144
28145/* Helper function for resolve_addr, mark DW_TAG_base_type nodes
28146 referenced from typed stack ops and count how often they are used. */
28147
28148static void
28149mark_base_types (dw_loc_descr_ref loc)
28150{
28151 dw_die_ref base_type = NULL;
28152
28153 for (; loc; loc = loc->dw_loc_next)
28154 {
28155 switch (loc->dw_loc_opc)
28156 {
28157 case DW_OP_regval_type:
28158 case DW_OP_deref_type:
28159 case DW_OP_GNU_regval_type:
28160 case DW_OP_GNU_deref_type:
28161 base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
28162 break;
28163 case DW_OP_convert:
28164 case DW_OP_reinterpret:
28165 case DW_OP_GNU_convert:
28166 case DW_OP_GNU_reinterpret:
28167 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
28168 continue;
28169 /* FALLTHRU */
28170 case DW_OP_const_type:
28171 case DW_OP_GNU_const_type:
28172 base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
28173 break;
28174 case DW_OP_entry_value:
28175 case DW_OP_GNU_entry_value:
28176 mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
28177 continue;
28178 default:
28179 continue;
28180 }
28181 gcc_assert (base_type->die_parent == comp_unit_die ());
28182 if (base_type->die_mark)
28183 base_type->die_mark++;
28184 else
28185 {
28186 base_types.safe_push (base_type);
28187 base_type->die_mark = 1;
28188 }
28189 }
28190}
28191
28192/* Comparison function for sorting marked base types. */
28193
28194static int
28195base_type_cmp (const void *x, const void *y)
28196{
28197 dw_die_ref dx = *(const dw_die_ref *) x;
28198 dw_die_ref dy = *(const dw_die_ref *) y;
28199 unsigned int byte_size1, byte_size2;
28200 unsigned int encoding1, encoding2;
28201 unsigned int align1, align2;
28202 if (dx->die_mark > dy->die_mark)
28203 return -1;
28204 if (dx->die_mark < dy->die_mark)
28205 return 1;
28206 byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
28207 byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
28208 if (byte_size1 < byte_size2)
28209 return 1;
28210 if (byte_size1 > byte_size2)
28211 return -1;
28212 encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
28213 encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
28214 if (encoding1 < encoding2)
28215 return 1;
28216 if (encoding1 > encoding2)
28217 return -1;
28218 align1 = get_AT_unsigned (dx, DW_AT_alignment);
28219 align2 = get_AT_unsigned (dy, DW_AT_alignment);
28220 if (align1 < align2)
28221 return 1;
28222 if (align1 > align2)
28223 return -1;
28224 return 0;
28225}
28226
28227/* Move base types marked by mark_base_types as early as possible
28228 in the CU, sorted by decreasing usage count both to make the
28229 uleb128 references as small as possible and to make sure they
28230 will have die_offset already computed by calc_die_sizes when
28231 sizes of typed stack loc ops is computed. */
28232
28233static void
28234move_marked_base_types (void)
28235{
28236 unsigned int i;
28237 dw_die_ref base_type, die, c;
28238
28239 if (base_types.is_empty ())
28240 return;
28241
28242 /* Sort by decreasing usage count, they will be added again in that
28243 order later on. */
28244 base_types.qsort (base_type_cmp);
28245 die = comp_unit_die ();
28246 c = die->die_child;
28247 do
28248 {
28249 dw_die_ref prev = c;
28250 c = c->die_sib;
28251 while (c->die_mark)
28252 {
28253 remove_child_with_prev (c, prev);
28254 /* As base types got marked, there must be at least
28255 one node other than DW_TAG_base_type. */
28256 gcc_assert (die->die_child != NULL);
28257 c = prev->die_sib;
28258 }
28259 }
28260 while (c != die->die_child);
28261 gcc_assert (die->die_child);
28262 c = die->die_child;
28263 for (i = 0; base_types.iterate (i, &base_type); i++)
28264 {
28265 base_type->die_mark = 0;
28266 base_type->die_sib = c->die_sib;
28267 c->die_sib = base_type;
28268 c = base_type;
28269 }
28270}
28271
28272/* Helper function for resolve_addr, attempt to resolve
28273 one CONST_STRING, return true if successful. Similarly verify that
28274 SYMBOL_REFs refer to variables emitted in the current CU. */
28275
28276static bool
28277resolve_one_addr (rtx *addr)
28278{
28279 rtx rtl = *addr;
28280
28281 if (GET_CODE (rtl) == CONST_STRING)
28282 {
28283 size_t len = strlen (XSTR (rtl, 0)) + 1;
28284 tree t = build_string (len, XSTR (rtl, 0));
28285 tree tlen = size_int (len - 1);
28286 TREE_TYPE (t)
28287 = build_array_type (char_type_node, build_index_type (tlen));
28288 rtl = lookup_constant_def (t);
28289 if (!rtl || !MEM_P (rtl))
28290 return false;
28291 rtl = XEXP (rtl, 0);
28292 if (GET_CODE (rtl) == SYMBOL_REF
28293 && SYMBOL_REF_DECL (rtl)
28294 && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
28295 return false;
28296 vec_safe_push (used_rtx_array, rtl);
28297 *addr = rtl;
28298 return true;
28299 }
28300
28301 if (GET_CODE (rtl) == SYMBOL_REF
28302 && SYMBOL_REF_DECL (rtl))
28303 {
28304 if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
28305 {
28306 if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
28307 return false;
28308 }
28309 else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
28310 return false;
28311 }
28312
28313 if (GET_CODE (rtl) == CONST)
28314 {
28315 subrtx_ptr_iterator::array_type array;
28316 FOR_EACH_SUBRTX_PTR (iter, array, &XEXP (rtl, 0), ALL)
28317 if (!resolve_one_addr (*iter))
28318 return false;
28319 }
28320
28321 return true;
28322}
28323
28324/* For STRING_CST, return SYMBOL_REF of its constant pool entry,
28325 if possible, and create DW_TAG_dwarf_procedure that can be referenced
28326 from DW_OP_implicit_pointer if the string hasn't been seen yet. */
28327
28328static rtx
28329string_cst_pool_decl (tree t)
28330{
28331 rtx rtl = output_constant_def (t, 1);
28332 unsigned char *array;
28333 dw_loc_descr_ref l;
28334 tree decl;
28335 size_t len;
28336 dw_die_ref ref;
28337
28338 if (!rtl || !MEM_P (rtl))
28339 return NULL_RTX;
28340 rtl = XEXP (rtl, 0);
28341 if (GET_CODE (rtl) != SYMBOL_REF
28342 || SYMBOL_REF_DECL (rtl) == NULL_TREE)
28343 return NULL_RTX;
28344
28345 decl = SYMBOL_REF_DECL (rtl);
28346 if (!lookup_decl_die (decl))
28347 {
28348 len = TREE_STRING_LENGTH (t);
28349 vec_safe_push (used_rtx_array, rtl);
28350 ref = new_die (DW_TAG_dwarf_procedure, comp_unit_die (), decl);
28351 array = ggc_vec_alloc<unsigned char> (len);
28352 memcpy (array, TREE_STRING_POINTER (t), len);
28353 l = new_loc_descr (DW_OP_implicit_value, len, 0);
28354 l->dw_loc_oprnd2.val_class = dw_val_class_vec;
28355 l->dw_loc_oprnd2.v.val_vec.length = len;
28356 l->dw_loc_oprnd2.v.val_vec.elt_size = 1;
28357 l->dw_loc_oprnd2.v.val_vec.array = array;
28358 add_AT_loc (ref, DW_AT_location, l);
28359 equate_decl_number_to_die (decl, ref);
28360 }
28361 return rtl;
28362}
28363
28364/* Helper function of resolve_addr_in_expr. LOC is
28365 a DW_OP_addr followed by DW_OP_stack_value, either at the start
28366 of exprloc or after DW_OP_{,bit_}piece, and val_addr can't be
28367 resolved. Replace it (both DW_OP_addr and DW_OP_stack_value)
28368 with DW_OP_implicit_pointer if possible
28369 and return true, if unsuccessful, return false. */
28370
28371static bool
28372optimize_one_addr_into_implicit_ptr (dw_loc_descr_ref loc)
28373{
28374 rtx rtl = loc->dw_loc_oprnd1.v.val_addr;
28375 HOST_WIDE_INT offset = 0;
28376 dw_die_ref ref = NULL;
28377 tree decl;
28378
28379 if (GET_CODE (rtl) == CONST
28380 && GET_CODE (XEXP (rtl, 0)) == PLUS
28381 && CONST_INT_P (XEXP (XEXP (rtl, 0), 1)))
28382 {
28383 offset = INTVAL (XEXP (XEXP (rtl, 0), 1));
28384 rtl = XEXP (XEXP (rtl, 0), 0);
28385 }
28386 if (GET_CODE (rtl) == CONST_STRING)
28387 {
28388 size_t len = strlen (XSTR (rtl, 0)) + 1;
28389 tree t = build_string (len, XSTR (rtl, 0));
28390 tree tlen = size_int (len - 1);
28391
28392 TREE_TYPE (t)
28393 = build_array_type (char_type_node, build_index_type (tlen));
28394 rtl = string_cst_pool_decl (t);
28395 if (!rtl)
28396 return false;
28397 }
28398 if (GET_CODE (rtl) == SYMBOL_REF && SYMBOL_REF_DECL (rtl))
28399 {
28400 decl = SYMBOL_REF_DECL (rtl);
28401 if (VAR_P (decl) && !DECL_EXTERNAL (decl))
28402 {
28403 ref = lookup_decl_die (decl);
28404 if (ref && (get_AT (ref, DW_AT_location)
28405 || get_AT (ref, DW_AT_const_value)))
28406 {
28407 loc->dw_loc_opc = dwarf_OP (DW_OP_implicit_pointer);
28408 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
28409 loc->dw_loc_oprnd1.val_entry = NULL;
28410 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
28411 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
28412 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
28413 loc->dw_loc_oprnd2.v.val_int = offset;
28414 return true;
28415 }
28416 }
28417 }
28418 return false;
28419}
28420
28421/* Helper function for resolve_addr, handle one location
28422 expression, return false if at least one CONST_STRING or SYMBOL_REF in
28423 the location list couldn't be resolved. */
28424
28425static bool
28426resolve_addr_in_expr (dw_attr_node *a, dw_loc_descr_ref loc)
28427{
28428 dw_loc_descr_ref keep = NULL;
28429 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = loc->dw_loc_next)
28430 switch (loc->dw_loc_opc)
28431 {
28432 case DW_OP_addr:
28433 if (!resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
28434 {
28435 if ((prev == NULL
28436 || prev->dw_loc_opc == DW_OP_piece
28437 || prev->dw_loc_opc == DW_OP_bit_piece)
28438 && loc->dw_loc_next
28439 && loc->dw_loc_next->dw_loc_opc == DW_OP_stack_value
28440 && (!dwarf_strict || dwarf_version >= 5)
28441 && optimize_one_addr_into_implicit_ptr (loc))
28442 break;
28443 return false;
28444 }
28445 break;
28446 case DW_OP_GNU_addr_index:
28447 case DW_OP_GNU_const_index:
28448 if (loc->dw_loc_opc == DW_OP_GNU_addr_index
28449 || (loc->dw_loc_opc == DW_OP_GNU_const_index && loc->dtprel))
28450 {
28451 rtx rtl = loc->dw_loc_oprnd1.val_entry->addr.rtl;
28452 if (!resolve_one_addr (&rtl))
28453 return false;
28454 remove_addr_table_entry (loc->dw_loc_oprnd1.val_entry);
28455 loc->dw_loc_oprnd1.val_entry
28456 = add_addr_table_entry (rtl, ate_kind_rtx);
28457 }
28458 break;
28459 case DW_OP_const4u:
28460 case DW_OP_const8u:
28461 if (loc->dtprel
28462 && !resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
28463 return false;
28464 break;
28465 case DW_OP_plus_uconst:
28466 if (size_of_loc_descr (loc)
28467 > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
28468 + 1
28469 && loc->dw_loc_oprnd1.v.val_unsigned > 0)
28470 {
28471 dw_loc_descr_ref repl
28472 = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
28473 add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
28474 add_loc_descr (&repl, loc->dw_loc_next);
28475 *loc = *repl;
28476 }
28477 break;
28478 case DW_OP_implicit_value:
28479 if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
28480 && !resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr))
28481 return false;
28482 break;
28483 case DW_OP_implicit_pointer:
28484 case DW_OP_GNU_implicit_pointer:
28485 case DW_OP_GNU_parameter_ref:
28486 case DW_OP_GNU_variable_value:
28487 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
28488 {
28489 dw_die_ref ref
28490 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
28491 if (ref == NULL)
28492 return false;
28493 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
28494 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
28495 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
28496 }
28497 if (loc->dw_loc_opc == DW_OP_GNU_variable_value)
28498 {
28499 if (prev == NULL
28500 && loc->dw_loc_next == NULL
28501 && AT_class (a) == dw_val_class_loc)
28502 switch (a->dw_attr)
28503 {
28504 /* Following attributes allow both exprloc and reference,
28505 so if the whole expression is DW_OP_GNU_variable_value
28506 alone we could transform it into reference. */
28507 case DW_AT_byte_size:
28508 case DW_AT_bit_size:
28509 case DW_AT_lower_bound:
28510 case DW_AT_upper_bound:
28511 case DW_AT_bit_stride:
28512 case DW_AT_count:
28513 case DW_AT_allocated:
28514 case DW_AT_associated:
28515 case DW_AT_byte_stride:
28516 a->dw_attr_val.val_class = dw_val_class_die_ref;
28517 a->dw_attr_val.val_entry = NULL;
28518 a->dw_attr_val.v.val_die_ref.die
28519 = loc->dw_loc_oprnd1.v.val_die_ref.die;
28520 a->dw_attr_val.v.val_die_ref.external = 0;
28521 return true;
28522 default:
28523 break;
28524 }
28525 if (dwarf_strict)
28526 return false;
28527 }
28528 break;
28529 case DW_OP_const_type:
28530 case DW_OP_regval_type:
28531 case DW_OP_deref_type:
28532 case DW_OP_convert:
28533 case DW_OP_reinterpret:
28534 case DW_OP_GNU_const_type:
28535 case DW_OP_GNU_regval_type:
28536 case DW_OP_GNU_deref_type:
28537 case DW_OP_GNU_convert:
28538 case DW_OP_GNU_reinterpret:
28539 while (loc->dw_loc_next
28540 && (loc->dw_loc_next->dw_loc_opc == DW_OP_convert
28541 || loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert))
28542 {
28543 dw_die_ref base1, base2;
28544 unsigned enc1, enc2, size1, size2;
28545 if (loc->dw_loc_opc == DW_OP_regval_type
28546 || loc->dw_loc_opc == DW_OP_deref_type
28547 || loc->dw_loc_opc == DW_OP_GNU_regval_type
28548 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
28549 base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
28550 else if (loc->dw_loc_oprnd1.val_class
28551 == dw_val_class_unsigned_const)
28552 break;
28553 else
28554 base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
28555 if (loc->dw_loc_next->dw_loc_oprnd1.val_class
28556 == dw_val_class_unsigned_const)
28557 break;
28558 base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
28559 gcc_assert (base1->die_tag == DW_TAG_base_type
28560 && base2->die_tag == DW_TAG_base_type);
28561 enc1 = get_AT_unsigned (base1, DW_AT_encoding);
28562 enc2 = get_AT_unsigned (base2, DW_AT_encoding);
28563 size1 = get_AT_unsigned (base1, DW_AT_byte_size);
28564 size2 = get_AT_unsigned (base2, DW_AT_byte_size);
28565 if (size1 == size2
28566 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
28567 && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
28568 && loc != keep)
28569 || enc1 == enc2))
28570 {
28571 /* Optimize away next DW_OP_convert after
28572 adjusting LOC's base type die reference. */
28573 if (loc->dw_loc_opc == DW_OP_regval_type
28574 || loc->dw_loc_opc == DW_OP_deref_type
28575 || loc->dw_loc_opc == DW_OP_GNU_regval_type
28576 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
28577 loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
28578 else
28579 loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
28580 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
28581 continue;
28582 }
28583 /* Don't change integer DW_OP_convert after e.g. floating
28584 point typed stack entry. */
28585 else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
28586 keep = loc->dw_loc_next;
28587 break;
28588 }
28589 break;
28590 default:
28591 break;
28592 }
28593 return true;
28594}
28595
28596/* Helper function of resolve_addr. DIE had DW_AT_location of
28597 DW_OP_addr alone, which referred to DECL in DW_OP_addr's operand
28598 and DW_OP_addr couldn't be resolved. resolve_addr has already
28599 removed the DW_AT_location attribute. This function attempts to
28600 add a new DW_AT_location attribute with DW_OP_implicit_pointer
28601 to it or DW_AT_const_value attribute, if possible. */
28602
28603static void
28604optimize_location_into_implicit_ptr (dw_die_ref die, tree decl)
28605{
28606 if (!VAR_P (decl)
28607 || lookup_decl_die (decl) != die
28608 || DECL_EXTERNAL (decl)
28609 || !TREE_STATIC (decl)
28610 || DECL_INITIAL (decl) == NULL_TREE
28611 || DECL_P (DECL_INITIAL (decl))
28612 || get_AT (die, DW_AT_const_value))
28613 return;
28614
28615 tree init = DECL_INITIAL (decl);
28616 HOST_WIDE_INT offset = 0;
28617 /* For variables that have been optimized away and thus
28618 don't have a memory location, see if we can emit
28619 DW_AT_const_value instead. */
28620 if (tree_add_const_value_attribute (die, init))
28621 return;
28622 if (dwarf_strict && dwarf_version < 5)
28623 return;
28624 /* If init is ADDR_EXPR or POINTER_PLUS_EXPR of ADDR_EXPR,
28625 and ADDR_EXPR refers to a decl that has DW_AT_location or
28626 DW_AT_const_value (but isn't addressable, otherwise
28627 resolving the original DW_OP_addr wouldn't fail), see if
28628 we can add DW_OP_implicit_pointer. */
28629 STRIP_NOPS (init);
28630 if (TREE_CODE (init) == POINTER_PLUS_EXPR
28631 && tree_fits_shwi_p (TREE_OPERAND (init, 1)))
28632 {
28633 offset = tree_to_shwi (TREE_OPERAND (init, 1));
28634 init = TREE_OPERAND (init, 0);
28635 STRIP_NOPS (init);
28636 }
28637 if (TREE_CODE (init) != ADDR_EXPR)
28638 return;
28639 if ((TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST
28640 && !TREE_ASM_WRITTEN (TREE_OPERAND (init, 0)))
28641 || (TREE_CODE (TREE_OPERAND (init, 0)) == VAR_DECL
28642 && !DECL_EXTERNAL (TREE_OPERAND (init, 0))
28643 && TREE_OPERAND (init, 0) != decl))
28644 {
28645 dw_die_ref ref;
28646 dw_loc_descr_ref l;
28647
28648 if (TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST)
28649 {
28650 rtx rtl = string_cst_pool_decl (TREE_OPERAND (init, 0));
28651 if (!rtl)
28652 return;
28653 decl = SYMBOL_REF_DECL (rtl);
28654 }
28655 else
28656 decl = TREE_OPERAND (init, 0);
28657 ref = lookup_decl_die (decl);
28658 if (ref == NULL
28659 || (!get_AT (ref, DW_AT_location)
28660 && !get_AT (ref, DW_AT_const_value)))
28661 return;
28662 l = new_loc_descr (dwarf_OP (DW_OP_implicit_pointer), 0, offset);
28663 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
28664 l->dw_loc_oprnd1.v.val_die_ref.die = ref;
28665 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
28666 add_AT_loc (die, DW_AT_location, l);
28667 }
28668}
28669
28670/* Return NULL if l is a DWARF expression, or first op that is not
28671 valid DWARF expression. */
28672
28673static dw_loc_descr_ref
28674non_dwarf_expression (dw_loc_descr_ref l)
28675{
28676 while (l)
28677 {
28678 if (l->dw_loc_opc >= DW_OP_reg0 && l->dw_loc_opc <= DW_OP_reg31)
28679 return l;
28680 switch (l->dw_loc_opc)
28681 {
28682 case DW_OP_regx:
28683 case DW_OP_implicit_value:
28684 case DW_OP_stack_value:
28685 case DW_OP_implicit_pointer:
28686 case DW_OP_GNU_implicit_pointer:
28687 case DW_OP_GNU_parameter_ref:
28688 case DW_OP_piece:
28689 case DW_OP_bit_piece:
28690 return l;
28691 default:
28692 break;
28693 }
28694 l = l->dw_loc_next;
28695 }
28696 return NULL;
28697}
28698
28699/* Return adjusted copy of EXPR:
28700 If it is empty DWARF expression, return it.
28701 If it is valid non-empty DWARF expression,
28702 return copy of EXPR with DW_OP_deref appended to it.
28703 If it is DWARF expression followed by DW_OP_reg{N,x}, return
28704 copy of the DWARF expression with DW_OP_breg{N,x} <0> appended.
28705 If it is DWARF expression followed by DW_OP_stack_value, return
28706 copy of the DWARF expression without anything appended.
28707 Otherwise, return NULL. */
28708
28709static dw_loc_descr_ref
28710copy_deref_exprloc (dw_loc_descr_ref expr)
28711{
28712 dw_loc_descr_ref tail = NULL;
28713
28714 if (expr == NULL)
28715 return NULL;
28716
28717 dw_loc_descr_ref l = non_dwarf_expression (expr);
28718 if (l && l->dw_loc_next)
28719 return NULL;
28720
28721 if (l)
28722 {
28723 if (l->dw_loc_opc >= DW_OP_reg0 && l->dw_loc_opc <= DW_OP_reg31)
28724 tail = new_loc_descr ((enum dwarf_location_atom)
28725 (DW_OP_breg0 + (l->dw_loc_opc - DW_OP_reg0)),
28726 0, 0);
28727 else
28728 switch (l->dw_loc_opc)
28729 {
28730 case DW_OP_regx:
28731 tail = new_loc_descr (DW_OP_bregx,
28732 l->dw_loc_oprnd1.v.val_unsigned, 0);
28733 break;
28734 case DW_OP_stack_value:
28735 break;
28736 default:
28737 return NULL;
28738 }
28739 }
28740 else
28741 tail = new_loc_descr (DW_OP_deref, 0, 0);
28742
28743 dw_loc_descr_ref ret = NULL, *p = &ret;
28744 while (expr != l)
28745 {
28746 *p = new_loc_descr (expr->dw_loc_opc, 0, 0);
28747 (*p)->dw_loc_oprnd1 = expr->dw_loc_oprnd1;
28748 (*p)->dw_loc_oprnd2 = expr->dw_loc_oprnd2;
28749 p = &(*p)->dw_loc_next;
28750 expr = expr->dw_loc_next;
28751 }
28752 *p = tail;
28753 return ret;
28754}
28755
28756/* For DW_AT_string_length attribute with DW_OP_GNU_variable_value
28757 reference to a variable or argument, adjust it if needed and return:
28758 -1 if the DW_AT_string_length attribute and DW_AT_{string_length_,}byte_size
28759 attribute if present should be removed
28760 0 keep the attribute perhaps with minor modifications, no need to rescan
28761 1 if the attribute has been successfully adjusted. */
28762
28763static int
28764optimize_string_length (dw_attr_node *a)
28765{
28766 dw_loc_descr_ref l = AT_loc (a), lv;
28767 dw_die_ref die;
28768 if (l->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
28769 {
28770 tree decl = l->dw_loc_oprnd1.v.val_decl_ref;
28771 die = lookup_decl_die (decl);
28772 if (die)
28773 {
28774 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
28775 l->dw_loc_oprnd1.v.val_die_ref.die = die;
28776 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
28777 }
28778 else
28779 return -1;
28780 }
28781 else
28782 die = l->dw_loc_oprnd1.v.val_die_ref.die;
28783
28784 /* DWARF5 allows reference class, so we can then reference the DIE.
28785 Only do this for DW_OP_GNU_variable_value DW_OP_stack_value. */
28786 if (l->dw_loc_next != NULL && dwarf_version >= 5)
28787 {
28788 a->dw_attr_val.val_class = dw_val_class_die_ref;
28789 a->dw_attr_val.val_entry = NULL;
28790 a->dw_attr_val.v.val_die_ref.die = die;
28791 a->dw_attr_val.v.val_die_ref.external = 0;
28792 return 0;
28793 }
28794
28795 dw_attr_node *av = get_AT (die, DW_AT_location);
28796 dw_loc_list_ref d;
28797 bool non_dwarf_expr = false;
28798
28799 if (av == NULL)
28800 return dwarf_strict ? -1 : 0;
28801 switch (AT_class (av))
28802 {
28803 case dw_val_class_loc_list:
28804 for (d = AT_loc_list (av); d != NULL; d = d->dw_loc_next)
28805 if (d->expr && non_dwarf_expression (d->expr))
28806 non_dwarf_expr = true;
28807 break;
28808 case dw_val_class_loc:
28809 lv = AT_loc (av);
28810 if (lv == NULL)
28811 return dwarf_strict ? -1 : 0;
28812 if (non_dwarf_expression (lv))
28813 non_dwarf_expr = true;
28814 break;
28815 default:
28816 return dwarf_strict ? -1 : 0;
28817 }
28818
28819 /* If it is safe to transform DW_OP_GNU_variable_value DW_OP_stack_value
28820 into DW_OP_call4 or DW_OP_GNU_variable_value into
28821 DW_OP_call4 DW_OP_deref, do so. */
28822 if (!non_dwarf_expr
28823 && (l->dw_loc_next != NULL || AT_class (av) == dw_val_class_loc))
28824 {
28825 l->dw_loc_opc = DW_OP_call4;
28826 if (l->dw_loc_next)
28827 l->dw_loc_next = NULL;
28828 else
28829 l->dw_loc_next = new_loc_descr (DW_OP_deref, 0, 0);
28830 return 0;
28831 }
28832
28833 /* For DW_OP_GNU_variable_value DW_OP_stack_value, we can just
28834 copy over the DW_AT_location attribute from die to a. */
28835 if (l->dw_loc_next != NULL)
28836 {
28837 a->dw_attr_val = av->dw_attr_val;
28838 return 1;
28839 }
28840
28841 dw_loc_list_ref list, *p;
28842 switch (AT_class (av))
28843 {
28844 case dw_val_class_loc_list:
28845 p = &list;
28846 list = NULL;
28847 for (d = AT_loc_list (av); d != NULL; d = d->dw_loc_next)
28848 {
28849 lv = copy_deref_exprloc (d->expr);
28850 if (lv)
28851 {
28852 *p = new_loc_list (lv, d->begin, d->end, d->section);
28853 p = &(*p)->dw_loc_next;
28854 }
28855 else if (!dwarf_strict && d->expr)
28856 return 0;
28857 }
28858 if (list == NULL)
28859 return dwarf_strict ? -1 : 0;
28860 a->dw_attr_val.val_class = dw_val_class_loc_list;
28861 gen_llsym (list);
28862 *AT_loc_list_ptr (a) = list;
28863 return 1;
28864 case dw_val_class_loc:
28865 lv = copy_deref_exprloc (AT_loc (av));
28866 if (lv == NULL)
28867 return dwarf_strict ? -1 : 0;
28868 a->dw_attr_val.v.val_loc = lv;
28869 return 1;
28870 default:
28871 gcc_unreachable ();
28872 }
28873}
28874
28875/* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
28876 an address in .rodata section if the string literal is emitted there,
28877 or remove the containing location list or replace DW_AT_const_value
28878 with DW_AT_location and empty location expression, if it isn't found
28879 in .rodata. Similarly for SYMBOL_REFs, keep only those that refer
28880 to something that has been emitted in the current CU. */
28881
28882static void
28883resolve_addr (dw_die_ref die)
28884{
28885 dw_die_ref c;
28886 dw_attr_node *a;
28887 dw_loc_list_ref *curr, *start, loc;
28888 unsigned ix;
28889 bool remove_AT_byte_size = false;
28890
28891 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
28892 switch (AT_class (a))
28893 {
28894 case dw_val_class_loc_list:
28895 start = curr = AT_loc_list_ptr (a);
28896 loc = *curr;
28897 gcc_assert (loc);
28898 /* The same list can be referenced more than once. See if we have
28899 already recorded the result from a previous pass. */
28900 if (loc->replaced)
28901 *curr = loc->dw_loc_next;
28902 else if (!loc->resolved_addr)
28903 {
28904 /* As things stand, we do not expect or allow one die to
28905 reference a suffix of another die's location list chain.
28906 References must be identical or completely separate.
28907 There is therefore no need to cache the result of this
28908 pass on any list other than the first; doing so
28909 would lead to unnecessary writes. */
28910 while (*curr)
28911 {
28912 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
28913 if (!resolve_addr_in_expr (a, (*curr)->expr))
28914 {
28915 dw_loc_list_ref next = (*curr)->dw_loc_next;
28916 dw_loc_descr_ref l = (*curr)->expr;
28917
28918 if (next && (*curr)->ll_symbol)
28919 {
28920 gcc_assert (!next->ll_symbol);
28921 next->ll_symbol = (*curr)->ll_symbol;
28922 }
28923 if (dwarf_split_debug_info)
28924 remove_loc_list_addr_table_entries (l);
28925 *curr = next;
28926 }
28927 else
28928 {
28929 mark_base_types ((*curr)->expr);
28930 curr = &(*curr)->dw_loc_next;
28931 }
28932 }
28933 if (loc == *start)
28934 loc->resolved_addr = 1;
28935 else
28936 {
28937 loc->replaced = 1;
28938 loc->dw_loc_next = *start;
28939 }
28940 }
28941 if (!*start)
28942 {
28943 remove_AT (die, a->dw_attr);
28944 ix--;
28945 }
28946 break;
28947 case dw_val_class_loc:
28948 {
28949 dw_loc_descr_ref l = AT_loc (a);
28950 /* DW_OP_GNU_variable_value DW_OP_stack_value or
28951 DW_OP_GNU_variable_value in DW_AT_string_length can be converted
28952 into DW_OP_call4 or DW_OP_call4 DW_OP_deref, which is standard
28953 DWARF4 unlike DW_OP_GNU_variable_value. Or for DWARF5
28954 DW_OP_GNU_variable_value DW_OP_stack_value can be replaced
28955 with DW_FORM_ref referencing the same DIE as
28956 DW_OP_GNU_variable_value used to reference. */
28957 if (a->dw_attr == DW_AT_string_length
28958 && l
28959 && l->dw_loc_opc == DW_OP_GNU_variable_value
28960 && (l->dw_loc_next == NULL
28961 || (l->dw_loc_next->dw_loc_next == NULL
28962 && l->dw_loc_next->dw_loc_opc == DW_OP_stack_value)))
28963 {
28964 switch (optimize_string_length (a))
28965 {
28966 case -1:
28967 remove_AT (die, a->dw_attr);
28968 ix--;
28969 /* If we drop DW_AT_string_length, we need to drop also
28970 DW_AT_{string_length_,}byte_size. */
28971 remove_AT_byte_size = true;
28972 continue;
28973 default:
28974 break;
28975 case 1:
28976 /* Even if we keep the optimized DW_AT_string_length,
28977 it might have changed AT_class, so process it again. */
28978 ix--;
28979 continue;
28980 }
28981 }
28982 /* For -gdwarf-2 don't attempt to optimize
28983 DW_AT_data_member_location containing
28984 DW_OP_plus_uconst - older consumers might
28985 rely on it being that op instead of a more complex,
28986 but shorter, location description. */
28987 if ((dwarf_version > 2
28988 || a->dw_attr != DW_AT_data_member_location
28989 || l == NULL
28990 || l->dw_loc_opc != DW_OP_plus_uconst
28991 || l->dw_loc_next != NULL)
28992 && !resolve_addr_in_expr (a, l))
28993 {
28994 if (dwarf_split_debug_info)
28995 remove_loc_list_addr_table_entries (l);
28996 if (l != NULL
28997 && l->dw_loc_next == NULL
28998 && l->dw_loc_opc == DW_OP_addr
28999 && GET_CODE (l->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF
29000 && SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr)
29001 && a->dw_attr == DW_AT_location)
29002 {
29003 tree decl = SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr);
29004 remove_AT (die, a->dw_attr);
29005 ix--;
29006 optimize_location_into_implicit_ptr (die, decl);
29007 break;
29008 }
29009 if (a->dw_attr == DW_AT_string_length)
29010 /* If we drop DW_AT_string_length, we need to drop also
29011 DW_AT_{string_length_,}byte_size. */
29012 remove_AT_byte_size = true;
29013 remove_AT (die, a->dw_attr);
29014 ix--;
29015 }
29016 else
29017 mark_base_types (l);
29018 }
29019 break;
29020 case dw_val_class_addr:
29021 if (a->dw_attr == DW_AT_const_value
29022 && !resolve_one_addr (&a->dw_attr_val.v.val_addr))
29023 {
29024 if (AT_index (a) != NOT_INDEXED)
29025 remove_addr_table_entry (a->dw_attr_val.val_entry);
29026 remove_AT (die, a->dw_attr);
29027 ix--;
29028 }
29029 if ((die->die_tag == DW_TAG_call_site
29030 && a->dw_attr == DW_AT_call_origin)
29031 || (die->die_tag == DW_TAG_GNU_call_site
29032 && a->dw_attr == DW_AT_abstract_origin))
29033 {
29034 tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
29035 dw_die_ref tdie = lookup_decl_die (tdecl);
29036 dw_die_ref cdie;
29037 if (tdie == NULL
29038 && DECL_EXTERNAL (tdecl)
29039 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE
29040 && (cdie = lookup_context_die (DECL_CONTEXT (tdecl))))
29041 {
29042 dw_die_ref pdie = cdie;
29043 /* Make sure we don't add these DIEs into type units.
29044 We could emit skeleton DIEs for context (namespaces,
29045 outer structs/classes) and a skeleton DIE for the
29046 innermost context with DW_AT_signature pointing to the
29047 type unit. See PR78835. */
29048 while (pdie && pdie->die_tag != DW_TAG_type_unit)
29049 pdie = pdie->die_parent;
29050 if (pdie == NULL)
29051 {
29052 /* Creating a full DIE for tdecl is overly expensive and
29053 at this point even wrong when in the LTO phase
29054 as it can end up generating new type DIEs we didn't
29055 output and thus optimize_external_refs will crash. */
29056 tdie = new_die (DW_TAG_subprogram, cdie, NULL_TREE);
29057 add_AT_flag (tdie, DW_AT_external, 1);
29058 add_AT_flag (tdie, DW_AT_declaration, 1);
29059 add_linkage_attr (tdie, tdecl);
29060 add_name_and_src_coords_attributes (tdie, tdecl);
29061 equate_decl_number_to_die (tdecl, tdie);
29062 }
29063 }
29064 if (tdie)
29065 {
29066 a->dw_attr_val.val_class = dw_val_class_die_ref;
29067 a->dw_attr_val.v.val_die_ref.die = tdie;
29068 a->dw_attr_val.v.val_die_ref.external = 0;
29069 }
29070 else
29071 {
29072 if (AT_index (a) != NOT_INDEXED)
29073 remove_addr_table_entry (a->dw_attr_val.val_entry);
29074 remove_AT (die, a->dw_attr);
29075 ix--;
29076 }
29077 }
29078 break;
29079 default:
29080 break;
29081 }
29082
29083 if (remove_AT_byte_size)
29084 remove_AT (die, dwarf_version >= 5
29085 ? DW_AT_string_length_byte_size
29086 : DW_AT_byte_size);
29087
29088 FOR_EACH_CHILD (die, c, resolve_addr (c));
29089}
29090
29091/* Helper routines for optimize_location_lists.
29092 This pass tries to share identical local lists in .debug_loc
29093 section. */
29094
29095/* Iteratively hash operands of LOC opcode into HSTATE. */
29096
29097static void
29098hash_loc_operands (dw_loc_descr_ref loc, inchash::hash &hstate)
29099{
29100 dw_val_ref val1 = &loc->dw_loc_oprnd1;
29101 dw_val_ref val2 = &loc->dw_loc_oprnd2;
29102
29103 switch (loc->dw_loc_opc)
29104 {
29105 case DW_OP_const4u:
29106 case DW_OP_const8u:
29107 if (loc->dtprel)
29108 goto hash_addr;
29109 /* FALLTHRU */
29110 case DW_OP_const1u:
29111 case DW_OP_const1s:
29112 case DW_OP_const2u:
29113 case DW_OP_const2s:
29114 case DW_OP_const4s:
29115 case DW_OP_const8s:
29116 case DW_OP_constu:
29117 case DW_OP_consts:
29118 case DW_OP_pick:
29119 case DW_OP_plus_uconst:
29120 case DW_OP_breg0:
29121 case DW_OP_breg1:
29122 case DW_OP_breg2:
29123 case DW_OP_breg3:
29124 case DW_OP_breg4:
29125 case DW_OP_breg5:
29126 case DW_OP_breg6:
29127 case DW_OP_breg7:
29128 case DW_OP_breg8:
29129 case DW_OP_breg9:
29130 case DW_OP_breg10:
29131 case DW_OP_breg11:
29132 case DW_OP_breg12:
29133 case DW_OP_breg13:
29134 case DW_OP_breg14:
29135 case DW_OP_breg15:
29136 case DW_OP_breg16:
29137 case DW_OP_breg17:
29138 case DW_OP_breg18:
29139 case DW_OP_breg19:
29140 case DW_OP_breg20:
29141 case DW_OP_breg21:
29142 case DW_OP_breg22:
29143 case DW_OP_breg23:
29144 case DW_OP_breg24:
29145 case DW_OP_breg25:
29146 case DW_OP_breg26:
29147 case DW_OP_breg27:
29148 case DW_OP_breg28:
29149 case DW_OP_breg29:
29150 case DW_OP_breg30:
29151 case DW_OP_breg31:
29152 case DW_OP_regx:
29153 case DW_OP_fbreg:
29154 case DW_OP_piece:
29155 case DW_OP_deref_size:
29156 case DW_OP_xderef_size:
29157 hstate.add_object (val1->v.val_int);
29158 break;
29159 case DW_OP_skip:
29160 case DW_OP_bra:
29161 {
29162 int offset;
29163
29164 gcc_assert (val1->val_class == dw_val_class_loc);
29165 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
29166 hstate.add_object (offset);
29167 }
29168 break;
29169 case DW_OP_implicit_value:
29170 hstate.add_object (val1->v.val_unsigned);
29171 switch (val2->val_class)
29172 {
29173 case dw_val_class_const:
29174 hstate.add_object (val2->v.val_int);
29175 break;
29176 case dw_val_class_vec:
29177 {
29178 unsigned int elt_size = val2->v.val_vec.elt_size;
29179 unsigned int len = val2->v.val_vec.length;
29180
29181 hstate.add_int (elt_size);
29182 hstate.add_int (len);
29183 hstate.add (val2->v.val_vec.array, len * elt_size);
29184 }
29185 break;
29186 case dw_val_class_const_double:
29187 hstate.add_object (val2->v.val_double.low);
29188 hstate.add_object (val2->v.val_double.high);
29189 break;
29190 case dw_val_class_wide_int:
29191 hstate.add (val2->v.val_wide->get_val (),
29192 get_full_len (*val2->v.val_wide)
29193 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
29194 break;
29195 case dw_val_class_addr:
29196 inchash::add_rtx (val2->v.val_addr, hstate);
29197 break;
29198 default:
29199 gcc_unreachable ();
29200 }
29201 break;
29202 case DW_OP_bregx:
29203 case DW_OP_bit_piece:
29204 hstate.add_object (val1->v.val_int);
29205 hstate.add_object (val2->v.val_int);
29206 break;
29207 case DW_OP_addr:
29208 hash_addr:
29209 if (loc->dtprel)
29210 {
29211 unsigned char dtprel = 0xd1;
29212 hstate.add_object (dtprel);
29213 }
29214 inchash::add_rtx (val1->v.val_addr, hstate);
29215 break;
29216 case DW_OP_GNU_addr_index:
29217 case DW_OP_GNU_const_index:
29218 {
29219 if (loc->dtprel)
29220 {
29221 unsigned char dtprel = 0xd1;
29222 hstate.add_object (dtprel);
29223 }
29224 inchash::add_rtx (val1->val_entry->addr.rtl, hstate);
29225 }
29226 break;
29227 case DW_OP_implicit_pointer:
29228 case DW_OP_GNU_implicit_pointer:
29229 hstate.add_int (val2->v.val_int);
29230 break;
29231 case DW_OP_entry_value:
29232 case DW_OP_GNU_entry_value:
29233 hstate.add_object (val1->v.val_loc);
29234 break;
29235 case DW_OP_regval_type:
29236 case DW_OP_deref_type:
29237 case DW_OP_GNU_regval_type:
29238 case DW_OP_GNU_deref_type:
29239 {
29240 unsigned int byte_size
29241 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
29242 unsigned int encoding
29243 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
29244 hstate.add_object (val1->v.val_int);
29245 hstate.add_object (byte_size);
29246 hstate.add_object (encoding);
29247 }
29248 break;
29249 case DW_OP_convert:
29250 case DW_OP_reinterpret:
29251 case DW_OP_GNU_convert:
29252 case DW_OP_GNU_reinterpret:
29253 if (val1->val_class == dw_val_class_unsigned_const)
29254 {
29255 hstate.add_object (val1->v.val_unsigned);
29256 break;
29257 }
29258 /* FALLTHRU */
29259 case DW_OP_const_type:
29260 case DW_OP_GNU_const_type:
29261 {
29262 unsigned int byte_size
29263 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
29264 unsigned int encoding
29265 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
29266 hstate.add_object (byte_size);
29267 hstate.add_object (encoding);
29268 if (loc->dw_loc_opc != DW_OP_const_type
29269 && loc->dw_loc_opc != DW_OP_GNU_const_type)
29270 break;
29271 hstate.add_object (val2->val_class);
29272 switch (val2->val_class)
29273 {
29274 case dw_val_class_const:
29275 hstate.add_object (val2->v.val_int);
29276 break;
29277 case dw_val_class_vec:
29278 {
29279 unsigned int elt_size = val2->v.val_vec.elt_size;
29280 unsigned int len = val2->v.val_vec.length;
29281
29282 hstate.add_object (elt_size);
29283 hstate.add_object (len);
29284 hstate.add (val2->v.val_vec.array, len * elt_size);
29285 }
29286 break;
29287 case dw_val_class_const_double:
29288 hstate.add_object (val2->v.val_double.low);
29289 hstate.add_object (val2->v.val_double.high);
29290 break;
29291 case dw_val_class_wide_int:
29292 hstate.add (val2->v.val_wide->get_val (),
29293 get_full_len (*val2->v.val_wide)
29294 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
29295 break;
29296 default:
29297 gcc_unreachable ();
29298 }
29299 }
29300 break;
29301
29302 default:
29303 /* Other codes have no operands. */
29304 break;
29305 }
29306}
29307
29308/* Iteratively hash the whole DWARF location expression LOC into HSTATE. */
29309
29310static inline void
29311hash_locs (dw_loc_descr_ref loc, inchash::hash &hstate)
29312{
29313 dw_loc_descr_ref l;
29314 bool sizes_computed = false;
29315 /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed. */
29316 size_of_locs (loc);
29317
29318 for (l = loc; l != NULL; l = l->dw_loc_next)
29319 {
29320 enum dwarf_location_atom opc = l->dw_loc_opc;
29321 hstate.add_object (opc);
29322 if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
29323 {
29324 size_of_locs (loc);
29325 sizes_computed = true;
29326 }
29327 hash_loc_operands (l, hstate);
29328 }
29329}
29330
29331/* Compute hash of the whole location list LIST_HEAD. */
29332
29333static inline void
29334hash_loc_list (dw_loc_list_ref list_head)
29335{
29336 dw_loc_list_ref curr = list_head;
29337 inchash::hash hstate;
29338
29339 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
29340 {
29341 hstate.add (curr->begin, strlen (curr->begin) + 1);
29342 hstate.add (curr->end, strlen (curr->end) + 1);
29343 if (curr->section)
29344 hstate.add (curr->section, strlen (curr->section) + 1);
29345 hash_locs (curr->expr, hstate);
29346 }
29347 list_head->hash = hstate.end ();
29348}
29349
29350/* Return true if X and Y opcodes have the same operands. */
29351
29352static inline bool
29353compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
29354{
29355 dw_val_ref valx1 = &x->dw_loc_oprnd1;
29356 dw_val_ref valx2 = &x->dw_loc_oprnd2;
29357 dw_val_ref valy1 = &y->dw_loc_oprnd1;
29358 dw_val_ref valy2 = &y->dw_loc_oprnd2;
29359
29360 switch (x->dw_loc_opc)
29361 {
29362 case DW_OP_const4u:
29363 case DW_OP_const8u:
29364 if (x->dtprel)
29365 goto hash_addr;
29366 /* FALLTHRU */
29367 case DW_OP_const1u:
29368 case DW_OP_const1s:
29369 case DW_OP_const2u:
29370 case DW_OP_const2s:
29371 case DW_OP_const4s:
29372 case DW_OP_const8s:
29373 case DW_OP_constu:
29374 case DW_OP_consts:
29375 case DW_OP_pick:
29376 case DW_OP_plus_uconst:
29377 case DW_OP_breg0:
29378 case DW_OP_breg1:
29379 case DW_OP_breg2:
29380 case DW_OP_breg3:
29381 case DW_OP_breg4:
29382 case DW_OP_breg5:
29383 case DW_OP_breg6:
29384 case DW_OP_breg7:
29385 case DW_OP_breg8:
29386 case DW_OP_breg9:
29387 case DW_OP_breg10:
29388 case DW_OP_breg11:
29389 case DW_OP_breg12:
29390 case DW_OP_breg13:
29391 case DW_OP_breg14:
29392 case DW_OP_breg15:
29393 case DW_OP_breg16:
29394 case DW_OP_breg17:
29395 case DW_OP_breg18:
29396 case DW_OP_breg19:
29397 case DW_OP_breg20:
29398 case DW_OP_breg21:
29399 case DW_OP_breg22:
29400 case DW_OP_breg23:
29401 case DW_OP_breg24:
29402 case DW_OP_breg25:
29403 case DW_OP_breg26:
29404 case DW_OP_breg27:
29405 case DW_OP_breg28:
29406 case DW_OP_breg29:
29407 case DW_OP_breg30:
29408 case DW_OP_breg31:
29409 case DW_OP_regx:
29410 case DW_OP_fbreg:
29411 case DW_OP_piece:
29412 case DW_OP_deref_size:
29413 case DW_OP_xderef_size:
29414 return valx1->v.val_int == valy1->v.val_int;
29415 case DW_OP_skip:
29416 case DW_OP_bra:
29417 /* If splitting debug info, the use of DW_OP_GNU_addr_index
29418 can cause irrelevant differences in dw_loc_addr. */
29419 gcc_assert (valx1->val_class == dw_val_class_loc
29420 && valy1->val_class == dw_val_class_loc
29421 && (dwarf_split_debug_info
29422 || x->dw_loc_addr == y->dw_loc_addr));
29423 return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
29424 case DW_OP_implicit_value:
29425 if (valx1->v.val_unsigned != valy1->v.val_unsigned
29426 || valx2->val_class != valy2->val_class)
29427 return false;
29428 switch (valx2->val_class)
29429 {
29430 case dw_val_class_const:
29431 return valx2->v.val_int == valy2->v.val_int;
29432 case dw_val_class_vec:
29433 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
29434 && valx2->v.val_vec.length == valy2->v.val_vec.length
29435 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
29436 valx2->v.val_vec.elt_size
29437 * valx2->v.val_vec.length) == 0;
29438 case dw_val_class_const_double:
29439 return valx2->v.val_double.low == valy2->v.val_double.low
29440 && valx2->v.val_double.high == valy2->v.val_double.high;
29441 case dw_val_class_wide_int:
29442 return *valx2->v.val_wide == *valy2->v.val_wide;
29443 case dw_val_class_addr:
29444 return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
29445 default:
29446 gcc_unreachable ();
29447 }
29448 case DW_OP_bregx:
29449 case DW_OP_bit_piece:
29450 return valx1->v.val_int == valy1->v.val_int
29451 && valx2->v.val_int == valy2->v.val_int;
29452 case DW_OP_addr:
29453 hash_addr:
29454 return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
29455 case DW_OP_GNU_addr_index:
29456 case DW_OP_GNU_const_index:
29457 {
29458 rtx ax1 = valx1->val_entry->addr.rtl;
29459 rtx ay1 = valy1->val_entry->addr.rtl;
29460 return rtx_equal_p (ax1, ay1);
29461 }
29462 case DW_OP_implicit_pointer:
29463 case DW_OP_GNU_implicit_pointer:
29464 return valx1->val_class == dw_val_class_die_ref
29465 && valx1->val_class == valy1->val_class
29466 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
29467 && valx2->v.val_int == valy2->v.val_int;
29468 case DW_OP_entry_value:
29469 case DW_OP_GNU_entry_value:
29470 return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
29471 case DW_OP_const_type:
29472 case DW_OP_GNU_const_type:
29473 if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
29474 || valx2->val_class != valy2->val_class)
29475 return false;
29476 switch (valx2->val_class)
29477 {
29478 case dw_val_class_const:
29479 return valx2->v.val_int == valy2->v.val_int;
29480 case dw_val_class_vec:
29481 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
29482 && valx2->v.val_vec.length == valy2->v.val_vec.length
29483 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
29484 valx2->v.val_vec.elt_size
29485 * valx2->v.val_vec.length) == 0;
29486 case dw_val_class_const_double:
29487 return valx2->v.val_double.low == valy2->v.val_double.low
29488 && valx2->v.val_double.high == valy2->v.val_double.high;
29489 case dw_val_class_wide_int:
29490 return *valx2->v.val_wide == *valy2->v.val_wide;
29491 default:
29492 gcc_unreachable ();
29493 }
29494 case DW_OP_regval_type:
29495 case DW_OP_deref_type:
29496 case DW_OP_GNU_regval_type:
29497 case DW_OP_GNU_deref_type:
29498 return valx1->v.val_int == valy1->v.val_int
29499 && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
29500 case DW_OP_convert:
29501 case DW_OP_reinterpret:
29502 case DW_OP_GNU_convert:
29503 case DW_OP_GNU_reinterpret:
29504 if (valx1->val_class != valy1->val_class)
29505 return false;
29506 if (valx1->val_class == dw_val_class_unsigned_const)
29507 return valx1->v.val_unsigned == valy1->v.val_unsigned;
29508 return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
29509 case DW_OP_GNU_parameter_ref:
29510 return valx1->val_class == dw_val_class_die_ref
29511 && valx1->val_class == valy1->val_class
29512 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
29513 default:
29514 /* Other codes have no operands. */
29515 return true;
29516 }
29517}
29518
29519/* Return true if DWARF location expressions X and Y are the same. */
29520
29521static inline bool
29522compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
29523{
29524 for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
29525 if (x->dw_loc_opc != y->dw_loc_opc
29526 || x->dtprel != y->dtprel
29527 || !compare_loc_operands (x, y))
29528 break;
29529 return x == NULL && y == NULL;
29530}
29531
29532/* Hashtable helpers. */
29533
29534struct loc_list_hasher : nofree_ptr_hash <dw_loc_list_struct>
29535{
29536 static inline hashval_t hash (const dw_loc_list_struct *);
29537 static inline bool equal (const dw_loc_list_struct *,
29538 const dw_loc_list_struct *);
29539};
29540
29541/* Return precomputed hash of location list X. */
29542
29543inline hashval_t
29544loc_list_hasher::hash (const dw_loc_list_struct *x)
29545{
29546 return x->hash;
29547}
29548
29549/* Return true if location lists A and B are the same. */
29550
29551inline bool
29552loc_list_hasher::equal (const dw_loc_list_struct *a,
29553 const dw_loc_list_struct *b)
29554{
29555 if (a == b)
29556 return 1;
29557 if (a->hash != b->hash)
29558 return 0;
29559 for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
29560 if (strcmp (a->begin, b->begin) != 0
29561 || strcmp (a->end, b->end) != 0
29562 || (a->section == NULL) != (b->section == NULL)
29563 || (a->section && strcmp (a->section, b->section) != 0)
29564 || !compare_locs (a->expr, b->expr))
29565 break;
29566 return a == NULL && b == NULL;
29567}
29568
29569typedef hash_table<loc_list_hasher> loc_list_hash_type;
29570
29571
29572/* Recursively optimize location lists referenced from DIE
29573 children and share them whenever possible. */
29574
29575static void
29576optimize_location_lists_1 (dw_die_ref die, loc_list_hash_type *htab)
29577{
29578 dw_die_ref c;
29579 dw_attr_node *a;
29580 unsigned ix;
29581 dw_loc_list_struct **slot;
29582
29583 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
29584 if (AT_class (a) == dw_val_class_loc_list)
29585 {
29586 dw_loc_list_ref list = AT_loc_list (a);
29587 /* TODO: perform some optimizations here, before hashing
29588 it and storing into the hash table. */
29589 hash_loc_list (list);
29590 slot = htab->find_slot_with_hash (list, list->hash, INSERT);
29591 if (*slot == NULL)
29592 *slot = list;
29593 else
29594 a->dw_attr_val.v.val_loc_list = *slot;
29595 }
29596
29597 FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
29598}
29599
29600
29601/* Recursively assign each location list a unique index into the debug_addr
29602 section. */
29603
29604static void
29605index_location_lists (dw_die_ref die)
29606{
29607 dw_die_ref c;
29608 dw_attr_node *a;
29609 unsigned ix;
29610
29611 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
29612 if (AT_class (a) == dw_val_class_loc_list)
29613 {
29614 dw_loc_list_ref list = AT_loc_list (a);
29615 dw_loc_list_ref curr;
29616 for (curr = list; curr != NULL; curr = curr->dw_loc_next)
29617 {
29618 /* Don't index an entry that has already been indexed
29619 or won't be output. */
29620 if (curr->begin_entry != NULL
29621 || (strcmp (curr->begin, curr->end) == 0 && !curr->force))
29622 continue;
29623
29624 curr->begin_entry
29625 = add_addr_table_entry (xstrdup (curr->begin), ate_kind_label);
29626 }
29627 }
29628
29629 FOR_EACH_CHILD (die, c, index_location_lists (c));
29630}
29631
29632/* Optimize location lists referenced from DIE
29633 children and share them whenever possible. */
29634
29635static void
29636optimize_location_lists (dw_die_ref die)
29637{
29638 loc_list_hash_type htab (500);
29639 optimize_location_lists_1 (die, &htab);
29640}
29641
29642/* Traverse the limbo die list, and add parent/child links. The only
29643 dies without parents that should be here are concrete instances of
29644 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
29645 For concrete instances, we can get the parent die from the abstract
29646 instance. */
29647
29648static void
29649flush_limbo_die_list (void)
29650{
29651 limbo_die_node *node;
29652
29653 /* get_context_die calls force_decl_die, which can put new DIEs on the
29654 limbo list in LTO mode when nested functions are put in a different
29655 partition than that of their parent function. */
29656 while ((node = limbo_die_list))
29657 {
29658 dw_die_ref die = node->die;
29659 limbo_die_list = node->next;
29660
29661 if (die->die_parent == NULL)
29662 {
29663 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
29664
29665 if (origin && origin->die_parent)
29666 add_child_die (origin->die_parent, die);
29667 else if (is_cu_die (die))
29668 ;
29669 else if (seen_error ())
29670 /* It's OK to be confused by errors in the input. */
29671 add_child_die (comp_unit_die (), die);
29672 else
29673 {
29674 /* In certain situations, the lexical block containing a
29675 nested function can be optimized away, which results
29676 in the nested function die being orphaned. Likewise
29677 with the return type of that nested function. Force
29678 this to be a child of the containing function.
29679
29680 It may happen that even the containing function got fully
29681 inlined and optimized out. In that case we are lost and
29682 assign the empty child. This should not be big issue as
29683 the function is likely unreachable too. */
29684 gcc_assert (node->created_for);
29685
29686 if (DECL_P (node->created_for))
29687 origin = get_context_die (DECL_CONTEXT (node->created_for));
29688 else if (TYPE_P (node->created_for))
29689 origin = scope_die_for (node->created_for, comp_unit_die ());
29690 else
29691 origin = comp_unit_die ();
29692
29693 add_child_die (origin, die);
29694 }
29695 }
29696 }
29697}
29698
29699/* Output stuff that dwarf requires at the end of every file,
29700 and generate the DWARF-2 debugging info. */
29701
29702static void
29703dwarf2out_finish (const char *)
29704{
29705 comdat_type_node *ctnode;
29706 dw_die_ref main_comp_unit_die;
29707 unsigned char checksum[16];
29708 char dl_section_ref[MAX_ARTIFICIAL_LABEL_BYTES];
29709
29710 /* Flush out any latecomers to the limbo party. */
29711 flush_limbo_die_list ();
29712
29713 if (flag_checking)
29714 {
29715 verify_die (comp_unit_die ());
29716 for (limbo_die_node *node = cu_die_list; node; node = node->next)
29717 verify_die (node->die);
29718 }
29719
29720 /* We shouldn't have any symbols with delayed asm names for
29721 DIEs generated after early finish. */
29722 gcc_assert (deferred_asm_name == NULL);
29723
29724 gen_remaining_tmpl_value_param_die_attribute ();
29725
29726#if ENABLE_ASSERT_CHECKING
29727 {
29728 dw_die_ref die = comp_unit_die (), c;
29729 FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
29730 }
29731#endif
29732 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
29733 resolve_addr (ctnode->root_die);
29734 resolve_addr (comp_unit_die ());
29735 move_marked_base_types ();
29736
29737 /* Initialize sections and labels used for actual assembler output. */
29738 init_sections_and_labels ();
29739
29740 /* Traverse the DIE's and add sibling attributes to those DIE's that
29741 have children. */
29742 add_sibling_attributes (comp_unit_die ());
29743 limbo_die_node *node;
29744 for (node = cu_die_list; node; node = node->next)
29745 add_sibling_attributes (node->die);
29746 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
29747 add_sibling_attributes (ctnode->root_die);
29748
29749 /* When splitting DWARF info, we put some attributes in the
29750 skeleton compile_unit DIE that remains in the .o, while
29751 most attributes go in the DWO compile_unit_die. */
29752 if (dwarf_split_debug_info)
29753 {
29754 limbo_die_node *cu;
29755 main_comp_unit_die = gen_compile_unit_die (NULL);
29756 if (dwarf_version >= 5)
29757 main_comp_unit_die->die_tag = DW_TAG_skeleton_unit;
29758 cu = limbo_die_list;
29759 gcc_assert (cu->die == main_comp_unit_die);
29760 limbo_die_list = limbo_die_list->next;
29761 cu->next = cu_die_list;
29762 cu_die_list = cu;
29763 }
29764 else
29765 main_comp_unit_die = comp_unit_die ();
29766
29767 /* Output a terminator label for the .text section. */
29768 switch_to_section (text_section);
29769 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
29770 if (cold_text_section)
29771 {
29772 switch_to_section (cold_text_section);
29773 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
29774 }
29775
29776 /* We can only use the low/high_pc attributes if all of the code was
29777 in .text. */
29778 if (!have_multiple_function_sections
29779 || (dwarf_version < 3 && dwarf_strict))
29780 {
29781 /* Don't add if the CU has no associated code. */
29782 if (text_section_used)
29783 add_AT_low_high_pc (main_comp_unit_die, text_section_label,
29784 text_end_label, true);
29785 }
29786 else
29787 {
29788 unsigned fde_idx;
29789 dw_fde_ref fde;
29790 bool range_list_added = false;
29791
29792 if (text_section_used)
29793 add_ranges_by_labels (main_comp_unit_die, text_section_label,
29794 text_end_label, &range_list_added, true);
29795 if (cold_text_section_used)
29796 add_ranges_by_labels (main_comp_unit_die, cold_text_section_label,
29797 cold_end_label, &range_list_added, true);
29798
29799 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
29800 {
29801 if (DECL_IGNORED_P (fde->decl))
29802 continue;
29803 if (!fde->in_std_section)
29804 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_begin,
29805 fde->dw_fde_end, &range_list_added,
29806 true);
29807 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
29808 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_second_begin,
29809 fde->dw_fde_second_end, &range_list_added,
29810 true);
29811 }
29812
29813 if (range_list_added)
29814 {
29815 /* We need to give .debug_loc and .debug_ranges an appropriate
29816 "base address". Use zero so that these addresses become
29817 absolute. Historically, we've emitted the unexpected
29818 DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
29819 Emit both to give time for other tools to adapt. */
29820 add_AT_addr (main_comp_unit_die, DW_AT_low_pc, const0_rtx, true);
29821 if (! dwarf_strict && dwarf_version < 4)
29822 add_AT_addr (main_comp_unit_die, DW_AT_entry_pc, const0_rtx, true);
29823
29824 add_ranges (NULL);
29825 }
29826 }
29827
29828 /* AIX Assembler inserts the length, so adjust the reference to match the
29829 offset expected by debuggers. */
29830 strcpy (dl_section_ref, debug_line_section_label);
29831 if (XCOFF_DEBUGGING_INFO)
29832 strcat (dl_section_ref, DWARF_INITIAL_LENGTH_SIZE_STR);
29833
29834 if (debug_info_level >= DINFO_LEVEL_TERSE)
29835 add_AT_lineptr (main_comp_unit_die, DW_AT_stmt_list,
29836 dl_section_ref);
29837
29838 if (have_macinfo)
29839 add_AT_macptr (comp_unit_die (),
29840 dwarf_version >= 5 ? DW_AT_macros
29841 : dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros,
29842 macinfo_section_label);
29843
29844 if (dwarf_split_debug_info)
29845 {
29846 if (have_location_lists)
29847 {
29848 if (dwarf_version >= 5)
29849 add_AT_loclistsptr (comp_unit_die (), DW_AT_loclists_base,
29850 loc_section_label);
29851 /* optimize_location_lists calculates the size of the lists,
29852 so index them first, and assign indices to the entries.
29853 Although optimize_location_lists will remove entries from
29854 the table, it only does so for duplicates, and therefore
29855 only reduces ref_counts to 1. */
29856 index_location_lists (comp_unit_die ());
29857 }
29858
29859 if (addr_index_table != NULL)
29860 {
29861 unsigned int index = 0;
29862 addr_index_table
29863 ->traverse_noresize<unsigned int *, index_addr_table_entry>
29864 (&index);
29865 }
29866 }
29867
29868 loc_list_idx = 0;
29869 if (have_location_lists)
29870 {
29871 optimize_location_lists (comp_unit_die ());
29872 /* And finally assign indexes to the entries for -gsplit-dwarf. */
29873 if (dwarf_version >= 5 && dwarf_split_debug_info)
29874 assign_location_list_indexes (comp_unit_die ());
29875 }
29876
29877 save_macinfo_strings ();
29878
29879 if (dwarf_split_debug_info)
29880 {
29881 unsigned int index = 0;
29882
29883 /* Add attributes common to skeleton compile_units and
29884 type_units. Because these attributes include strings, it
29885 must be done before freezing the string table. Top-level
29886 skeleton die attrs are added when the skeleton type unit is
29887 created, so ensure it is created by this point. */
29888 add_top_level_skeleton_die_attrs (main_comp_unit_die);
29889 debug_str_hash->traverse_noresize<unsigned int *, index_string> (&index);
29890 }
29891
29892 /* Output all of the compilation units. We put the main one last so that
29893 the offsets are available to output_pubnames. */
29894 for (node = cu_die_list; node; node = node->next)
29895 output_comp_unit (node->die, 0, NULL);
29896
29897 hash_table<comdat_type_hasher> comdat_type_table (100);
29898 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
29899 {
29900 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
29901
29902 /* Don't output duplicate types. */
29903 if (*slot != HTAB_EMPTY_ENTRY)
29904 continue;
29905
29906 /* Add a pointer to the line table for the main compilation unit
29907 so that the debugger can make sense of DW_AT_decl_file
29908 attributes. */
29909 if (debug_info_level >= DINFO_LEVEL_TERSE)
29910 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
29911 (!dwarf_split_debug_info
29912 ? dl_section_ref
29913 : debug_skeleton_line_section_label));
29914
29915 output_comdat_type_unit (ctnode);
29916 *slot = ctnode;
29917 }
29918
29919 /* The AT_pubnames attribute needs to go in all skeleton dies, including
29920 both the main_cu and all skeleton TUs. Making this call unconditional
29921 would end up either adding a second copy of the AT_pubnames attribute, or
29922 requiring a special case in add_top_level_skeleton_die_attrs. */
29923 if (!dwarf_split_debug_info)
29924 add_AT_pubnames (comp_unit_die ());
29925
29926 if (dwarf_split_debug_info)
29927 {
29928 int mark;
29929 struct md5_ctx ctx;
29930
29931 if (dwarf_version >= 5 && !vec_safe_is_empty (ranges_table))
29932 index_rnglists ();
29933
29934 /* Compute a checksum of the comp_unit to use as the dwo_id. */
29935 md5_init_ctx (&ctx);
29936 mark = 0;
29937 die_checksum (comp_unit_die (), &ctx, &mark);
29938 unmark_all_dies (comp_unit_die ());
29939 md5_finish_ctx (&ctx, checksum);
29940
29941 if (dwarf_version < 5)
29942 {
29943 /* Use the first 8 bytes of the checksum as the dwo_id,
29944 and add it to both comp-unit DIEs. */
29945 add_AT_data8 (main_comp_unit_die, DW_AT_GNU_dwo_id, checksum);
29946 add_AT_data8 (comp_unit_die (), DW_AT_GNU_dwo_id, checksum);
29947 }
29948
29949 /* Add the base offset of the ranges table to the skeleton
29950 comp-unit DIE. */
29951 if (!vec_safe_is_empty (ranges_table))
29952 {
29953 if (dwarf_version >= 5)
29954 add_AT_lineptr (main_comp_unit_die, DW_AT_rnglists_base,
29955 ranges_base_label);
29956 else
29957 add_AT_lineptr (main_comp_unit_die, DW_AT_GNU_ranges_base,
29958 ranges_section_label);
29959 }
29960
29961 switch_to_section (debug_addr_section);
29962 ASM_OUTPUT_LABEL (asm_out_file, debug_addr_section_label);
29963 output_addr_table ();
29964 }
29965
29966 /* Output the main compilation unit if non-empty or if .debug_macinfo
29967 or .debug_macro will be emitted. */
29968 output_comp_unit (comp_unit_die (), have_macinfo,
29969 dwarf_split_debug_info ? checksum : NULL);
29970
29971 if (dwarf_split_debug_info && info_section_emitted)
29972 output_skeleton_debug_sections (main_comp_unit_die, checksum);
29973
29974 /* Output the abbreviation table. */
29975 if (vec_safe_length (abbrev_die_table) != 1)
29976 {
29977 switch_to_section (debug_abbrev_section);
29978 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
29979 output_abbrev_section ();
29980 }
29981
29982 /* Output location list section if necessary. */
29983 if (have_location_lists)
29984 {
29985 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
29986 char l2[MAX_ARTIFICIAL_LABEL_BYTES];
29987 /* Output the location lists info. */
29988 switch_to_section (debug_loc_section);
29989 if (dwarf_version >= 5)
29990 {
29991 ASM_GENERATE_INTERNAL_LABEL (l1, DEBUG_LOC_SECTION_LABEL, 1);
29992 ASM_GENERATE_INTERNAL_LABEL (l2, DEBUG_LOC_SECTION_LABEL, 2);
29993 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
29994 dw2_asm_output_data (4, 0xffffffff,
29995 "Initial length escape value indicating "
29996 "64-bit DWARF extension");
29997 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
29998 "Length of Location Lists");
29999 ASM_OUTPUT_LABEL (asm_out_file, l1);
30000 dw2_asm_output_data (2, dwarf_version, "DWARF Version");
30001 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
30002 dw2_asm_output_data (1, 0, "Segment Size");
30003 dw2_asm_output_data (4, dwarf_split_debug_info ? loc_list_idx : 0,
30004 "Offset Entry Count");
30005 }
30006 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
30007 if (dwarf_version >= 5 && dwarf_split_debug_info)
30008 {
30009 unsigned int save_loc_list_idx = loc_list_idx;
30010 loc_list_idx = 0;
30011 output_loclists_offsets (comp_unit_die ());
30012 gcc_assert (save_loc_list_idx == loc_list_idx);
30013 }
30014 output_location_lists (comp_unit_die ());
30015 if (dwarf_version >= 5)
30016 ASM_OUTPUT_LABEL (asm_out_file, l2);
30017 }
30018
30019 output_pubtables ();
30020
30021 /* Output the address range information if a CU (.debug_info section)
30022 was emitted. We output an empty table even if we had no functions
30023 to put in it. This because the consumer has no way to tell the
30024 difference between an empty table that we omitted and failure to
30025 generate a table that would have contained data. */
30026 if (info_section_emitted)
30027 {
30028 switch_to_section (debug_aranges_section);
30029 output_aranges ();
30030 }
30031
30032 /* Output ranges section if necessary. */
30033 if (!vec_safe_is_empty (ranges_table))
30034 {
30035 if (dwarf_version >= 5)
30036 output_rnglists ();
30037 else
30038 output_ranges ();
30039 }
30040
30041 /* Have to end the macro section. */
30042 if (have_macinfo)
30043 {
30044 switch_to_section (debug_macinfo_section);
30045 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
30046 output_macinfo ();
30047 dw2_asm_output_data (1, 0, "End compilation unit");
30048 }
30049
30050 /* Output the source line correspondence table. We must do this
30051 even if there is no line information. Otherwise, on an empty
30052 translation unit, we will generate a present, but empty,
30053 .debug_info section. IRIX 6.5 `nm' will then complain when
30054 examining the file. This is done late so that any filenames
30055 used by the debug_info section are marked as 'used'. */
30056 switch_to_section (debug_line_section);
30057 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
30058 if (! DWARF2_ASM_LINE_DEBUG_INFO)
30059 output_line_info (false);
30060
30061 if (dwarf_split_debug_info && info_section_emitted)
30062 {
30063 switch_to_section (debug_skeleton_line_section);
30064 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
30065 output_line_info (true);
30066 }
30067
30068 /* If we emitted any indirect strings, output the string table too. */
30069 if (debug_str_hash || skeleton_debug_str_hash)
30070 output_indirect_strings ();
30071 if (debug_line_str_hash)
30072 {
30073 switch_to_section (debug_line_str_section);
30074 const enum dwarf_form form = DW_FORM_line_strp;
30075 debug_line_str_hash->traverse<enum dwarf_form,
30076 output_indirect_string> (form);
30077 }
30078}
30079
30080/* Returns a hash value for X (which really is a variable_value_struct). */
30081
30082inline hashval_t
30083variable_value_hasher::hash (variable_value_struct *x)
30084{
30085 return (hashval_t) x->decl_id;
30086}
30087
30088/* Return nonzero if decl_id of variable_value_struct X is the same as
30089 UID of decl Y. */
30090
30091inline bool
30092variable_value_hasher::equal (variable_value_struct *x, tree y)
30093{
30094 return x->decl_id == DECL_UID (y);
30095}
30096
30097/* Helper function for resolve_variable_value, handle
30098 DW_OP_GNU_variable_value in one location expression.
30099 Return true if exprloc has been changed into loclist. */
30100
30101static bool
30102resolve_variable_value_in_expr (dw_attr_node *a, dw_loc_descr_ref loc)
30103{
30104 dw_loc_descr_ref next;
30105 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = next)
30106 {
30107 next = loc->dw_loc_next;
30108 if (loc->dw_loc_opc != DW_OP_GNU_variable_value
30109 || loc->dw_loc_oprnd1.val_class != dw_val_class_decl_ref)
30110 continue;
30111
30112 tree decl = loc->dw_loc_oprnd1.v.val_decl_ref;
30113 if (DECL_CONTEXT (decl) != current_function_decl)
30114 continue;
30115
30116 dw_die_ref ref = lookup_decl_die (decl);
30117 if (ref)
30118 {
30119 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
30120 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
30121 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
30122 continue;
30123 }
30124 dw_loc_list_ref l = loc_list_from_tree (decl, 0, NULL);
30125 if (l == NULL)
30126 continue;
30127 if (l->dw_loc_next)
30128 {
30129 if (AT_class (a) != dw_val_class_loc)
30130 continue;
30131 switch (a->dw_attr)
30132 {
30133 /* Following attributes allow both exprloc and loclist
30134 classes, so we can change them into a loclist. */
30135 case DW_AT_location:
30136 case DW_AT_string_length:
30137 case DW_AT_return_addr:
30138 case DW_AT_data_member_location:
30139 case DW_AT_frame_base:
30140 case DW_AT_segment:
30141 case DW_AT_static_link:
30142 case DW_AT_use_location:
30143 case DW_AT_vtable_elem_location:
30144 if (prev)
30145 {
30146 prev->dw_loc_next = NULL;
30147 prepend_loc_descr_to_each (l, AT_loc (a));
30148 }
30149 if (next)
30150 add_loc_descr_to_each (l, next);
30151 a->dw_attr_val.val_class = dw_val_class_loc_list;
30152 a->dw_attr_val.val_entry = NULL;
30153 a->dw_attr_val.v.val_loc_list = l;
30154 have_location_lists = true;
30155 return true;
30156 /* Following attributes allow both exprloc and reference,
30157 so if the whole expression is DW_OP_GNU_variable_value alone
30158 we could transform it into reference. */
30159 case DW_AT_byte_size:
30160 case DW_AT_bit_size:
30161 case DW_AT_lower_bound:
30162 case DW_AT_upper_bound:
30163 case DW_AT_bit_stride:
30164 case DW_AT_count:
30165 case DW_AT_allocated:
30166 case DW_AT_associated:
30167 case DW_AT_byte_stride:
30168 if (prev == NULL && next == NULL)
30169 break;
30170 /* FALLTHRU */
30171 default:
30172 if (dwarf_strict)
30173 continue;
30174 break;
30175 }
30176 /* Create DW_TAG_variable that we can refer to. */
30177 ref = gen_decl_die (decl, NULL_TREE, NULL,
30178 lookup_decl_die (current_function_decl));
30179 if (ref)
30180 {
30181 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
30182 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
30183 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
30184 }
30185 continue;
30186 }
30187 if (prev)
30188 {
30189 prev->dw_loc_next = l->expr;
30190 add_loc_descr (&prev->dw_loc_next, next);
30191 free_loc_descr (loc, NULL);
30192 next = prev->dw_loc_next;
30193 }
30194 else
30195 {
30196 memcpy (loc, l->expr, sizeof (dw_loc_descr_node));
30197 add_loc_descr (&loc, next);
30198 next = loc;
30199 }
30200 loc = prev;
30201 }
30202 return false;
30203}
30204
30205/* Attempt to resolve DW_OP_GNU_variable_value using loc_list_from_tree. */
30206
30207static void
30208resolve_variable_value (dw_die_ref die)
30209{
30210 dw_attr_node *a;
30211 dw_loc_list_ref loc;
30212 unsigned ix;
30213
30214 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
30215 switch (AT_class (a))
30216 {
30217 case dw_val_class_loc:
30218 if (!resolve_variable_value_in_expr (a, AT_loc (a)))
30219 break;
30220 /* FALLTHRU */
30221 case dw_val_class_loc_list:
30222 loc = AT_loc_list (a);
30223 gcc_assert (loc);
30224 for (; loc; loc = loc->dw_loc_next)
30225 resolve_variable_value_in_expr (a, loc->expr);
30226 break;
30227 default:
30228 break;
30229 }
30230}
30231
30232/* Attempt to optimize DW_OP_GNU_variable_value refering to
30233 temporaries in the current function. */
30234
30235static void
30236resolve_variable_values (void)
30237{
30238 if (!variable_value_hash || !current_function_decl)
30239 return;
30240
30241 struct variable_value_struct *node
30242 = variable_value_hash->find_with_hash (current_function_decl,
30243 DECL_UID (current_function_decl));
30244
30245 if (node == NULL)
30246 return;
30247
30248 unsigned int i;
30249 dw_die_ref die;
30250 FOR_EACH_VEC_SAFE_ELT (node->dies, i, die)
30251 resolve_variable_value (die);
30252}
30253
30254/* Helper function for note_variable_value, handle one location
30255 expression. */
30256
30257static void
30258note_variable_value_in_expr (dw_die_ref die, dw_loc_descr_ref loc)
30259{
30260 for (; loc; loc = loc->dw_loc_next)
30261 if (loc->dw_loc_opc == DW_OP_GNU_variable_value
30262 && loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
30263 {
30264 tree decl = loc->dw_loc_oprnd1.v.val_decl_ref;
30265 dw_die_ref ref = lookup_decl_die (decl);
30266 if (ref)
30267 {
30268 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
30269 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
30270 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
30271 }
30272 if (VAR_P (decl)
30273 && DECL_CONTEXT (decl)
30274 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL
30275 && lookup_decl_die (DECL_CONTEXT (decl)))
30276 {
30277 if (!variable_value_hash)
30278 variable_value_hash
30279 = hash_table<variable_value_hasher>::create_ggc (10);
30280
30281 tree fndecl = DECL_CONTEXT (decl);
30282 struct variable_value_struct *node;
30283 struct variable_value_struct **slot
30284 = variable_value_hash->find_slot_with_hash (fndecl,
30285 DECL_UID (fndecl),
30286 INSERT);
30287 if (*slot == NULL)
30288 {
30289 node = ggc_cleared_alloc<variable_value_struct> ();
30290 node->decl_id = DECL_UID (fndecl);
30291 *slot = node;
30292 }
30293 else
30294 node = *slot;
30295
30296 vec_safe_push (node->dies, die);
30297 }
30298 }
30299}
30300
30301/* Walk the tree DIE and note DIEs with DW_OP_GNU_variable_value still
30302 with dw_val_class_decl_ref operand. */
30303
30304static void
30305note_variable_value (dw_die_ref die)
30306{
30307 dw_die_ref c;
30308 dw_attr_node *a;
30309 dw_loc_list_ref loc;
30310 unsigned ix;
30311
30312 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
30313 switch (AT_class (a))
30314 {
30315 case dw_val_class_loc_list:
30316 loc = AT_loc_list (a);
30317 gcc_assert (loc);
30318 if (!loc->noted_variable_value)
30319 {
30320 loc->noted_variable_value = 1;
30321 for (; loc; loc = loc->dw_loc_next)
30322 note_variable_value_in_expr (die, loc->expr);
30323 }
30324 break;
30325 case dw_val_class_loc:
30326 note_variable_value_in_expr (die, AT_loc (a));
30327 break;
30328 default:
30329 break;
30330 }
30331
30332 /* Mark children. */
30333 FOR_EACH_CHILD (die, c, note_variable_value (c));
30334}
30335
30336/* Perform any cleanups needed after the early debug generation pass
30337 has run. */
30338
30339static void
30340dwarf2out_early_finish (const char *filename)
30341{
30342 set_early_dwarf s;
30343
30344 /* PCH might result in DW_AT_producer string being restored from the
30345 header compilation, so always fill it with empty string initially
30346 and overwrite only here. */
30347 dw_attr_node *producer = get_AT (comp_unit_die (), DW_AT_producer);
30348 producer_string = gen_producer_string ();
30349 producer->dw_attr_val.v.val_str->refcount--;
30350 producer->dw_attr_val.v.val_str = find_AT_string (producer_string);
30351
30352 /* Add the name for the main input file now. We delayed this from
30353 dwarf2out_init to avoid complications with PCH. */
30354 add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
30355 add_comp_dir_attribute (comp_unit_die ());
30356
30357 /* When emitting DWARF5 .debug_line_str, move DW_AT_name and
30358 DW_AT_comp_dir into .debug_line_str section. */
30359 if (!DWARF2_ASM_LINE_DEBUG_INFO
30360 && dwarf_version >= 5
30361 && DWARF5_USE_DEBUG_LINE_STR)
30362 {
30363 for (int i = 0; i < 2; i++)
30364 {
30365 dw_attr_node *a = get_AT (comp_unit_die (),
30366 i ? DW_AT_comp_dir : DW_AT_name);
30367 if (a == NULL
30368 || AT_class (a) != dw_val_class_str
30369 || strlen (AT_string (a)) + 1 <= DWARF_OFFSET_SIZE)
30370 continue;
30371
30372 if (! debug_line_str_hash)
30373 debug_line_str_hash
30374 = hash_table<indirect_string_hasher>::create_ggc (10);
30375
30376 struct indirect_string_node *node
30377 = find_AT_string_in_table (AT_string (a), debug_line_str_hash);
30378 set_indirect_string (node);
30379 node->form = DW_FORM_line_strp;
30380 a->dw_attr_val.v.val_str->refcount--;
30381 a->dw_attr_val.v.val_str = node;
30382 }
30383 }
30384
30385 /* With LTO early dwarf was really finished at compile-time, so make
30386 sure to adjust the phase after annotating the LTRANS CU DIE. */
30387 if (in_lto_p)
30388 {
30389 early_dwarf_finished = true;
30390 return;
30391 }
30392
30393 /* Walk through the list of incomplete types again, trying once more to
30394 emit full debugging info for them. */
30395 retry_incomplete_types ();
30396
30397 /* The point here is to flush out the limbo list so that it is empty
30398 and we don't need to stream it for LTO. */
30399 flush_limbo_die_list ();
30400
30401 gen_scheduled_generic_parms_dies ();
30402 gen_remaining_tmpl_value_param_die_attribute ();
30403
30404 /* Add DW_AT_linkage_name for all deferred DIEs. */
30405 for (limbo_die_node *node = deferred_asm_name; node; node = node->next)
30406 {
30407 tree decl = node->created_for;
30408 if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
30409 /* A missing DECL_ASSEMBLER_NAME can be a constant DIE that
30410 ended up in deferred_asm_name before we knew it was
30411 constant and never written to disk. */
30412 && DECL_ASSEMBLER_NAME (decl))
30413 {
30414 add_linkage_attr (node->die, decl);
30415 move_linkage_attr (node->die);
30416 }
30417 }
30418 deferred_asm_name = NULL;
30419
30420 if (flag_eliminate_unused_debug_types)
30421 prune_unused_types ();
30422
30423 /* Generate separate COMDAT sections for type DIEs. */
30424 if (use_debug_types)
30425 {
30426 break_out_comdat_types (comp_unit_die ());
30427
30428 /* Each new type_unit DIE was added to the limbo die list when created.
30429 Since these have all been added to comdat_type_list, clear the
30430 limbo die list. */
30431 limbo_die_list = NULL;
30432
30433 /* For each new comdat type unit, copy declarations for incomplete
30434 types to make the new unit self-contained (i.e., no direct
30435 references to the main compile unit). */
30436 for (comdat_type_node *ctnode = comdat_type_list;
30437 ctnode != NULL; ctnode = ctnode->next)
30438 copy_decls_for_unworthy_types (ctnode->root_die);
30439 copy_decls_for_unworthy_types (comp_unit_die ());
30440
30441 /* In the process of copying declarations from one unit to another,
30442 we may have left some declarations behind that are no longer
30443 referenced. Prune them. */
30444 prune_unused_types ();
30445 }
30446
30447 /* Generate separate CUs for each of the include files we've seen.
30448 They will go into limbo_die_list and from there to cu_die_list. */
30449 if (flag_eliminate_dwarf2_dups)
30450 {
30451 gcc_assert (limbo_die_list == NULL);
30452 break_out_includes (comp_unit_die ());
30453 limbo_die_node *cu;
30454 while ((cu = limbo_die_list))
30455 {
30456 limbo_die_list = cu->next;
30457 cu->next = cu_die_list;
30458 cu_die_list = cu;
30459 }
30460 }
30461
30462 /* Traverse the DIE's and note DIEs with DW_OP_GNU_variable_value still
30463 with dw_val_class_decl_ref operand. */
30464 note_variable_value (comp_unit_die ());
30465 for (limbo_die_node *node = cu_die_list; node; node = node->next)
30466 note_variable_value (node->die);
30467 for (comdat_type_node *ctnode = comdat_type_list; ctnode != NULL;
30468 ctnode = ctnode->next)
30469 note_variable_value (ctnode->root_die);
30470 for (limbo_die_node *node = limbo_die_list; node; node = node->next)
30471 note_variable_value (node->die);
30472
30473 /* The early debug phase is now finished. */
30474 early_dwarf_finished = true;
30475}
30476
30477/* Reset all state within dwarf2out.c so that we can rerun the compiler
30478 within the same process. For use by toplev::finalize. */
30479
30480void
30481dwarf2out_c_finalize (void)
30482{
30483 last_var_location_insn = NULL;
30484 cached_next_real_insn = NULL;
30485 used_rtx_array = NULL;
30486 incomplete_types = NULL;
30487 decl_scope_table = NULL;
30488 debug_info_section = NULL;
30489 debug_skeleton_info_section = NULL;
30490 debug_abbrev_section = NULL;
30491 debug_skeleton_abbrev_section = NULL;
30492 debug_aranges_section = NULL;
30493 debug_addr_section = NULL;
30494 debug_macinfo_section = NULL;
30495 debug_line_section = NULL;
30496 debug_skeleton_line_section = NULL;
30497 debug_loc_section = NULL;
30498 debug_pubnames_section = NULL;
30499 debug_pubtypes_section = NULL;
30500 debug_str_section = NULL;
30501 debug_line_str_section = NULL;
30502 debug_str_dwo_section = NULL;
30503 debug_str_offsets_section = NULL;
30504 debug_ranges_section = NULL;
30505 debug_frame_section = NULL;
30506 fde_vec = NULL;
30507 debug_str_hash = NULL;
30508 debug_line_str_hash = NULL;
30509 skeleton_debug_str_hash = NULL;
30510 dw2_string_counter = 0;
30511 have_multiple_function_sections = false;
30512 text_section_used = false;
30513 cold_text_section_used = false;
30514 cold_text_section = NULL;
30515 current_unit_personality = NULL;
30516
30517 early_dwarf = false;
30518 early_dwarf_finished = false;
30519
30520 next_die_offset = 0;
30521 single_comp_unit_die = NULL;
30522 comdat_type_list = NULL;
30523 limbo_die_list = NULL;
30524 file_table = NULL;
30525 decl_die_table = NULL;
30526 common_block_die_table = NULL;
30527 decl_loc_table = NULL;
30528 call_arg_locations = NULL;
30529 call_arg_loc_last = NULL;
30530 call_site_count = -1;
30531 tail_call_site_count = -1;
30532 cached_dw_loc_list_table = NULL;
30533 abbrev_die_table = NULL;
30534 delete dwarf_proc_stack_usage_map;
30535 dwarf_proc_stack_usage_map = NULL;
30536 line_info_label_num = 0;
30537 cur_line_info_table = NULL;
30538 text_section_line_info = NULL;
30539 cold_text_section_line_info = NULL;
30540 separate_line_info = NULL;
30541 info_section_emitted = false;
30542 pubname_table = NULL;
30543 pubtype_table = NULL;
30544 macinfo_table = NULL;
30545 ranges_table = NULL;
30546 ranges_by_label = NULL;
30547 rnglist_idx = 0;
30548 have_location_lists = false;
30549 loclabel_num = 0;
30550 poc_label_num = 0;
30551 last_emitted_file = NULL;
30552 label_num = 0;
30553 tmpl_value_parm_die_table = NULL;
30554 generic_type_instances = NULL;
30555 frame_pointer_fb_offset = 0;
30556 frame_pointer_fb_offset_valid = false;
30557 base_types.release ();
30558 XDELETEVEC (producer_string);
30559 producer_string = NULL;
30560}
30561
30562#include "gt-dwarf2out.h"
30563